简体   繁体   中英

request.post() works at URL but yields 500 response when ran in python

When I copy and paste the value of payload1 into the API url, I get a 200 response. This is the URL:

https://api.usaspending.gov/api/v2/bulk_download/awards/

However, when I run the code within python via request.post(), I get a 500 response. My final goal is to get the returned zip file into python so I can automate the data pull. I'm new to APIs and some experience to python so I would like to keep the code as simple as possible. Thanks in advance.

import requests

payload1 = {
"filters": {
"prime_award_types": [
"A",
"B",
"C",
"D",
"IDV_A",
"IDV_B",
"IDV_B_A",
"IDV_B_B",
"IDV_B_C",
"IDV_C",
"IDV_D",
"IDV_E",
"02",
"03",
"04",
"05",
"10",
"06",
"07",
"08",
"09",
"11"
],
"agency": 66,
"date_type": "action_date",
"date_range": {"start_date":"2019-01-01","end_date":"2019-01-31"}
},
"columns": [],
"file_format": "csv"
}

response = requests.post('https://api.usaspending.gov/api/v2/bulk_download/awards/', data = payload1)

try to use the json keyword arg in the post method as payload:

url = "https://api.usaspending.gov/api/v2/bulk_download/awards/"
requests.post(url, json=payload1)

You can store payload in a file, load it into a variable and pass it to post function of requests library. So you would have be:

import requests
with open('desired_payload.txt','rt') as f:
    desired_payload = f.read()


url = 'https://api.usaspending.gov/api/v2/bulk_download/awards/'
headers = {'Content-Type': 'application/json'}
resp = requests.post(url, headers=headers, data=desired_payload)
if resp.status_code == 200:
    print('success')
    print(resp.content)
else:
    print('fail')

With this script i can receive a successful response that contains a status_url , file_name , file_url , ... for a zip file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM