Python post 请求 API
Python post request for an API
我是初学者,当 运行 从 API 端点提取数据的代码时,我遇到了 422 错误。我在 Google 上搜索了代码并意识到它是一个(不可处理的实体)状态代码,但我不确定如何修复它。
谁能告诉我如何修改我的代码?
import requests
url = "https://api.usaspending.gov"
endpoint = "/api/v2/search/spending_by_award"
criteria = {
"filters": {
"award_type_codes": ["10"],
"agencies": [
{
"type": "awarding",
"tier": "toptier",
"name": "Social Security Administration"
},
{
"type": "awarding",
"tier": "subtier",
"name": "Social Security Administration"
}
],
"legal_entities": [779928],
"recipient_scope": "domestic",
"recipient_locations": [650597],
"recipient_type_names": ["Individual"],
"place_of_performance_scope": "domestic",
"place_of_performance_locations": [60323],
"award_amounts": [
{
"lower_bound": 1500000.00,
"upper_bound": 1600000.00
}
],
"award_ids": [1018950]
},
"fields": ["Award ID", "Recipient Name", "Start Date", "End Date", "Award Amount", "Awarding Agency", "Awarding Sub Agency", "Award Type", "Funding Agency", "Funding Sub Agency"],
"sort": "Recipient Name",
"order": "desc"
}
response = requests.post(f"{url}{endpoint}", params=criteria)
print(response.status_code)
您可以修改几个字段的数据类型,即award_ids
应该是array[string]
,recipient_locations
由array[LocationObject]
组成
一个工作示例:
import requests
import json
url = "https://api.usaspending.gov/api/v2/search/spending_by_award"
payload = json.dumps({
"filters": {
"award_type_codes": [
"10"
],
"agencies": [
{
"type": "awarding",
"tier": "toptier",
"name": "Social Security Administration"
},
{
"type": "awarding",
"tier": "subtier",
"name": "Social Security Administration"
}
],
"legal_entities": [
779928
],
"recipient_scope": "domestic",
"recipient_type_names": [
"Individual"
],
"place_of_performance_scope": "domestic",
"award_amounts": [
{
"lower_bound": 1500000,
"upper_bound": 1600000
}
],
"award_ids": [
"1018950"
]
},
"fields": [
"Award ID",
"Recipient Name",
"Start Date",
"End Date",
"Award Amount",
"Awarding Agency",
"Awarding Sub Agency",
"Award Type",
"Funding Agency",
"Funding Sub Agency"
],
"sort": "Recipient Name",
"order": "desc"
})
headers = {
'Content-Type': 'application/json',
'Cookie': 'cookiesession1=678A3E0DCDEFGHIJKLNOPQRSTUV08936'
}
response = requests.request("POST", url, headers=headers, data=payload)
print(response.text)
print(response.status_code)
结果:
{
"limit": 10,
"results": [],
"page_metadata": {
"page": 1,
"hasNext": false,
"last_record_unique_id": null,
"last_record_sort_value": "None"
},
"messages": [
[
"For searches, time period start and end dates are currently limited to an earliest date of 2007-10-01. For data going back to 2000-10-01, use either the Custom Award Download feature on the website or one of our download or bulk_download API endpoints as listed on https://api.usaspending.gov/docs/endpoints."
]
]
}
我是初学者,当 运行 从 API 端点提取数据的代码时,我遇到了 422 错误。我在 Google 上搜索了代码并意识到它是一个(不可处理的实体)状态代码,但我不确定如何修复它。
谁能告诉我如何修改我的代码?
import requests
url = "https://api.usaspending.gov"
endpoint = "/api/v2/search/spending_by_award"
criteria = {
"filters": {
"award_type_codes": ["10"],
"agencies": [
{
"type": "awarding",
"tier": "toptier",
"name": "Social Security Administration"
},
{
"type": "awarding",
"tier": "subtier",
"name": "Social Security Administration"
}
],
"legal_entities": [779928],
"recipient_scope": "domestic",
"recipient_locations": [650597],
"recipient_type_names": ["Individual"],
"place_of_performance_scope": "domestic",
"place_of_performance_locations": [60323],
"award_amounts": [
{
"lower_bound": 1500000.00,
"upper_bound": 1600000.00
}
],
"award_ids": [1018950]
},
"fields": ["Award ID", "Recipient Name", "Start Date", "End Date", "Award Amount", "Awarding Agency", "Awarding Sub Agency", "Award Type", "Funding Agency", "Funding Sub Agency"],
"sort": "Recipient Name",
"order": "desc"
}
response = requests.post(f"{url}{endpoint}", params=criteria)
print(response.status_code)
您可以修改几个字段的数据类型,即award_ids
应该是array[string]
,recipient_locations
由array[LocationObject]
一个工作示例:
import requests
import json
url = "https://api.usaspending.gov/api/v2/search/spending_by_award"
payload = json.dumps({
"filters": {
"award_type_codes": [
"10"
],
"agencies": [
{
"type": "awarding",
"tier": "toptier",
"name": "Social Security Administration"
},
{
"type": "awarding",
"tier": "subtier",
"name": "Social Security Administration"
}
],
"legal_entities": [
779928
],
"recipient_scope": "domestic",
"recipient_type_names": [
"Individual"
],
"place_of_performance_scope": "domestic",
"award_amounts": [
{
"lower_bound": 1500000,
"upper_bound": 1600000
}
],
"award_ids": [
"1018950"
]
},
"fields": [
"Award ID",
"Recipient Name",
"Start Date",
"End Date",
"Award Amount",
"Awarding Agency",
"Awarding Sub Agency",
"Award Type",
"Funding Agency",
"Funding Sub Agency"
],
"sort": "Recipient Name",
"order": "desc"
})
headers = {
'Content-Type': 'application/json',
'Cookie': 'cookiesession1=678A3E0DCDEFGHIJKLNOPQRSTUV08936'
}
response = requests.request("POST", url, headers=headers, data=payload)
print(response.text)
print(response.status_code)
结果:
{
"limit": 10,
"results": [],
"page_metadata": {
"page": 1,
"hasNext": false,
"last_record_unique_id": null,
"last_record_sort_value": "None"
},
"messages": [
[
"For searches, time period start and end dates are currently limited to an earliest date of 2007-10-01. For data going back to 2000-10-01, use either the Custom Award Download feature on the website or one of our download or bulk_download API endpoints as listed on https://api.usaspending.gov/docs/endpoints."
]
]
}