使用python请求库在HTTP请求中发送json [英] Using python requests library to send json in http request
本文介绍了使用python请求库在HTTP请求中发送json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用python请求库发送http GET请求.以下是我的代码.
I am trying to send a http GET request using python requests library. Following is my code.
#!/usr/bin/python3
import requests
import json
URL = some-elkstack-url
datam = {'ayyo' : 'vammo'}
data_json = json.dumps(datam)
payload = {'json_payload': data_json}
header={'Content-Type': 'application/json' }
r = requests.get(url=URL, headers=header, data=datam)
a = r.json()
print('\nResponse: \n')
print(a)
我正在从服务器获取此HTTP错误.
I am getting this HTTP error back from the server.
{'error': {'root_cause': [{'type': 'json_parse_exception', 'reason': "Unrecognized token 'ayyo': was expecting ('true', 'false' or 'null')\n at
[Source: org.elasticsearch.transport.netty4.ByteBufStreamInput@74cef381; line: 1, column: 6]"}], 'type': 'json_parse_exception', 'reason': "Unrecognized token 'ayyo': was expecting ('true', 'false' or 'null')\n at
[Source: org.elasticsearch.transport.netty4.ByteBufStreamInput@74cef381; line: 1, column: 6]"}, 'status': 500}
当我在命令行中使用相同的json数据进行卷曲时,我可以获得正确的响应.我的代码出了什么问题?
When I do a curl from the command line, with the same json data I can get a proper response. What's going wrong in my code?
推荐答案
您要使用json参数代替数据,如下所示:
Instead of data, you want to use the json parameter, as follows:
datam = {'ayyo' : 'vammo'}
r = requests.get(URL,headers={'Content-Type': 'application/json' }, json=datam)
a = r.json()
# so on
希望有帮助!
这篇关于使用python请求库在HTTP请求中发送json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文