Reputation: 155
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)
Upvotes: 0
Views: 180
Reputation: 1418
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)
Upvotes: 3
Reputation: 161
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.
Upvotes: 0