本文介绍了将JSON字符串作为发布请求发送的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! rocksteady的解决方案他最初是指字典。但是下面的代码发送JSON字符串也使用请求工作奇迹:He did originally refer to dictionaries. But the following code to send the JSON string also worked wonders using requests:import requestsheaders = { '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 jsonfrom urllib.request import Request, urlopenfrom 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 withoutcontent = 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 requestscontent = {"title":"Read a book"}request = requests.get("http://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("http://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字符串作为发布请求发送的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-11 06:33