【问题标题】:Python post request for an API对 API 的 Python 发布请求
【发布时间】:2021-05-29 11:46:43
【问题描述】:

我是初学者,在运行代码以从 API 端点提取数据时遇到 422 错误。我用谷歌搜索了代码并意识到它是一个(无法处理的实体)状态代码,但我不确定如何修复它。

API 的文档就在这里:https://github.com/fedspendingtransparency/usaspending-api/blob/master/usaspending_api/api_contracts/contracts/v2/search/spending_by_award.md

谁能告诉我如何修改我的代码?

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)

【问题讨论】:

    标签: python api http-status-code-422


    【解决方案1】:

    您可以修改多个字段的数据类型,即award_ids应该是array[string]recipient_locationsarray[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."
            ]
        ]
    }
    
    
    

    【讨论】:

    • 非常感谢您的快速回复。代码正在运行!唯一的缺点是我试图从该站点获取数据以在熊猫中报告,但运行代码后我得到的唯一响应是: {"limit":10,"results":[],"page_metadata ":{"page":1,"hasNext":false,"last_record_unique_id":null,"last_record_sort_value":"None"},"messages":[["对于搜索,时间段的开始和结束日期目前仅限于最早的日期为 2007 年 10 月 1 日。对于可追溯到 2000 年 10 月 1 日等的数据。抱歉,如果这是一个牵强的问题,但知道如何获取组织的完整数据记录吗?
    • "results":[]表示您当前的参数没有数据,您可以尝试其他组合参数
    • 哦,我现在明白了,我会尝试更改参数,希望它会起作用!你是最棒的!
    猜你喜欢
    • 2020-02-09
    • 2022-08-13
    • 1970-01-01
    • 2014-10-14
    • 1970-01-01
    • 2020-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多