Python 3.4 请求正文格式错误 [英] Python 3.4 Requests Body wrong format

查看:46
本文介绍了Python 3.4 请求正文格式错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Python 3.4 中,我使用请求和 for 循环来组合多项目 API 请求的主体,以获取 JSON 内容.即使使用 type() 将 body 变量识别为 str 类,它也会产生 HTTP 400 错误.但是,如果我打印内容并将其复制到新变量中,则它是成功的.正在进行什么样的格式化?

导入请求,jsonlist_length = len(namelist) #namelist 在代码中排在前面有效载荷='['对于范围内的 x(0,list_length):有效载荷 += '{"name": "'+ namelist[x] + '"}'如果 x

解决方案

您正在创建一个 JSON 字符串,然后将其编码为 JSON 字符串.这种双重编码不是您在这里想要的:

<预><代码>>>>payload = '[{"name": "sune"}, {"name": "Demon"}, {"name": "kingenin"}]'>>>打印(json.dumps(有效负载))"[{\"name\": \"sune\"}, {\"name\": \"Demon\"}, {\"name\": \"kingenin\"}]"

这是一个 JSON 字符串,包含一个带引号的 JSON 列表..

构建一个列表,并将其传递给json.dumps():

payload = [{'name': name} for name in namelist]url = 'http://api.turfgame.com/v4/users'标头 = {'内容类型':'应用程序/json'}req = requests.post(url, data=json.dumps(payload),headers=headers)

这会发送一个正确的 JSON 列表:

<预><代码>>>>有效载荷[{'name': 'sune'}, {'name': 'Demon'}, {'name': 'kingenin'}]>>>打印(json.dumps(有效负载))[{"name": "sune"}, {"name": "Demon"}, {"name": "kingenin"}]

您也可以在构建它时发送 payload,而无需将其传递给 json.dumps(),但是为什么要养狗并自己吠叫呢?>

如果您要使用 requests 2.4.2 或更高版本,您可以让它为您处理 JSON 编码;将 Python 对象传入 json 关键字参数,它甚至会设置正确的 Content-Type 标头:

payload = [{'name': name} for name in namelist]url = 'http://api.turfgame.com/v4/users'req = requests.post(url, json=payload)

In Python 3.4 I'm using Requests and a for loop to combine the body for a multiple item API request fetching JSON content. It yields a HTTP 400 error even if the body variable is recognized as a str class with type(). However if I print and copy the content into a new variable it is successful. What kind of formatting is going on?

import requests,json
list_length = len(namelist) #namelist arranged earlier in code
payload='['
for x in range(0, list_length):
    payload += '{"name": "'+ namelist[x] + '"}'
    if x<list_length-1:
        payload += ', '
payload += ']'
url = 'http://api.turfgame.com/v4/users'
headers = {'Content-Type': 'application/json'}
req = requests.post(url, data=json.dumps(payload),headers=headers)

>>> payload
'[{"name": "sune"}, {"name": "Demon"}, {"name": "kingenin"}]'

解决方案

You are creating a JSON string, then encode it as a JSON string. This double encoding is not what you'll want here:

>>> payload = '[{"name": "sune"}, {"name": "Demon"}, {"name": "kingenin"}]'
>>> print(json.dumps(payload))
"[{\"name\": \"sune\"}, {\"name\": \"Demon\"}, {\"name\": \"kingenin\"}]"

That's a JSON string, containing a quoted JSON list..

Build a list, and pass that to json.dumps():

payload = [{'name': name} for name in namelist]
url = 'http://api.turfgame.com/v4/users'
headers = {'Content-Type': 'application/json'}
req = requests.post(url, data=json.dumps(payload),headers=headers)

This sends a proper JSON list instead:

>>> payload
[{'name': 'sune'}, {'name': 'Demon'}, {'name': 'kingenin'}]
>>> print(json.dumps(payload))
[{"name": "sune"}, {"name": "Demon"}, {"name": "kingenin"}]

You could also send payload as you built it, without passing it to json.dumps(), but why have a dog and bark yourself at all?

If you were to use requests version 2.4.2 or up, you can have it handle the JSON encoding for you; pass in the Python object into the json keyword argument and it'll even set the correct Content-Type header:

payload = [{'name': name} for name in namelist]
url = 'http://api.turfgame.com/v4/users'
req = requests.post(url, json=payload)

这篇关于Python 3.4 请求正文格式错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆