我遇到了一个奇怪的问题,Python post请求挂起,而cURL post请求工作正常。我向NIH Reporter API发出请求。
下面是一直挂着的Python代码:
headers = {
"accept": "application/json",
"Content-Type": "application/json"
}
data = {
"criteria": {
"award_notice_date": {
"from_date": "2022-06-22",
}
},
"include_fields": [
"ProjectNum",
"ProjectTitle",
"AbstractText",
"AwardNoticeDate"
],
"offset": 14500,
"limit": 500,
"sort_field": "award_notice_date"
}
response = requests.post('https://api.reporter.nih.gov/v2/projects/search', headers=headers, data=str(data))
下面是cURL的等效代码:
curl -X POST "https://api.reporter.nih.gov/v2/projects/search" -H "accept: application/json" -H "Content-Type: application/json" -d "{'criteria': {'award_notice_date': {'from_date': '2022-06-22'}}, 'include_fields': ['ProjectNum', 'ProjectTitle', 'AbstractText', 'AwardNoticeDate'], 'offset': 14500, 'limit': 500, 'sort_field': 'award_notice_date'}"
我不确定我是否遗漏了什么。我也在使用Python3,而且我的Requests库是最新的2.27.1版本。
1条答案
按热度按时间pxyaymoc1#
str(data)
和将其转换为json是不一样的,你需要使用data=json.dumps(data)
或者json=data
,使用后者也会为你添加"Content-Type": "application/json"
头,所以你可以跳过它。