rocksteady的解决方案
他最初是指字典。但是下面的代码发送JSON字符串也使用请求工作奇迹:
He did originally refer to dictionaries. But the following code to send the JSON string also worked wonders using requests:
import requests headers = { 'Authorization': app_token } url = api_url + "/b2api/v1/b2_get_upload_url" content = json.dumps({'bucketId': bucket_id}) r = requests.post(url, data = content, headers = headers)
我正在使用一个API,要求我发送JSON作为POST请求以获得结果。问题是Python 3不允许我这样做。
I'm working with an API that requires me to send JSON as a POST request to get results. Problem is that Python 3 won't allow me to do this.
以下Python 2代码工作正常,实际上它是官方样本:
The following Python 2 code works fine, in fact it's the official sample:
request = urllib2.Request( api_url +'/b2api/v1/b2_get_upload_url', json.dumps({ 'bucketId' : bucket_id }), headers = { 'Authorization': account_authorization_token } ) response = urllib2.urlopen(request)然而,在Python 3中使用此代码只会让人抱怨数据无效:
However, using this code in Python 3 only makes it complain about data being invalid:
import json from urllib.request import Request, urlopen from urllib.parse import urlencode # -! Irrelevant code has been cut out !- headers = { 'Authorization': app_token } url = api_url + "/b2api/v1/b2_get_upload_url" # Tested both with encode and without content = json.dumps({'bucketId': bucket_id}).encode('utf-8') request = Request( url=url, data=content, headers=headers ) response = urlopen(req)我试过做 urlencode(),就像你一样应该。但是这会从Web服务器返回400状态代码,因为它期望纯JSON。即使纯JSON数据无效,我也需要以某种方式强制Python发送它。
I've tried doing urlencode(), like you're supposed to. But this returns a 400 status code from the web server, because it's expecting pure JSON. Even if the pure JSON data is invalid, I need to somehow force Python into sending it.
编辑:根据要求,这里是错误我明白了由于这是一个烧瓶应用程序,这里是调试器的屏幕截图:
EDIT: As requested, here are the errors I get. Since this is a flask application, here's a screenshot of the debugger:
截图
添加 .encode('utf-8 ')给我一个预期字符串或缓冲区错误
Adding .encode('utf-8') gives me an "Expected string or buffer" error
编辑2 :调试器的屏幕截图,带有 .encode(' utf-8')已添加
推荐答案由于我有类似的应用程序正在运行,但客户端仍然失踪了,我自己试了一下。 正在运行的服务器来自以下练习:
Since I have a similar application running, but the client still was missing, I tried it myself. The server which is running is from the following exercise:
Miguel Grinberg - 使用Flask设计一个安静的API
这就是它使用身份验证的原因。
That's why it uses authentication.
但是有趣的部分:使用 requests 你可以保留字典原样。
But the interesting part: Using requests you can leave the dictionary as it is.
看看这个:
username = 'miguel' password = 'python' import requests content = {"title":"Read a book"} request = requests.get("127.0.0.1:5000/api/v1.0/projects", auth=(username, password), params=content) print request.text似乎有效:)
更新1:
POST请求是使用requests.post(...)完成的。这里描述得很好: python请求
POST requests are done using requests.post(...) This here describes it well : python requests
更新2:
为了完成答案:
requests.post("127.0.0.1:5000/api/v1.0/projects", json=content)发送json-st ring。
sends the json-string.
json 是请求的有效参数,内部使用 json.dumps () ...
json is a valid parameter of the request and internally uses json.dumps()...
更多推荐
将JSON字符串作为发布请求发送
发布评论