将JSON字符串作为发布请求发送

编程入门 行业动态 更新时间:2024-10-27 08:34:48
本文介绍了将JSON字符串作为发布请求发送的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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字符串作为发布请求发送

本文发布于:2023-10-12 00:00:37,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1483213.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   JSON

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!