0

This my first time hooking up to an API. I'm attempting to query NIH project data and can seem to hook up correctly to it as I get a status code of 200. The issue I have is when I try to print the output. I get a URL rejection despite having access. The documentation suggests that if any issues come up, it may be due to my IP being blocked due to a variety of reasons. I reached out to the API support team there and I don't have any issues with a blocked IP. They had me run some curl requests via command prompt and I was able to execute those correctly. This leads me to believe I have a code issue. What am I doing wrong here?

NIH API Info

Spyder output

import requests


params = {
     "criteria":
     {
       "fiscal_years":[2019,2018]
     },
     "include_fields": [
        "ApplId","SubprojectId","FiscalYear","Organization", "ProjectNum","OrgCountry",
        "ProjectNumSplit","ContactPiName","AllText","FullStudySection",
        "ProjectStartDate","ProjectEndDate"
     ],
     "offset":0,
     "limit":25,
     "sort_field":"project_start_date",
     "sort_order":"desc"
 }
response = requests.post("https://api.reporter.nih.gov/v2/projects/Search", data = params)

#print(response.status_code)

print(response.text)

Sample curl script and output:

curl -X POST "https://api.reporter.nih.gov/v2/projects/search" -H "accept: application/json" -H "Content-Type: application/json" -d "{​​​​​​​\"criteria\":{​​​​​​​\"covid_response\":[\"Reg-CV\",\"CV\"]}​​​​​​​,\"include_fields\":[\"ApplId\",\"SubprojectId\",\"FiscalYear\",\"Organization\",\"ProjectNum\",\"OrgCountry\",\"ProjectNumSplit\",\"ContactPiName\",\"AllText\",\"FullStudySection\",\"ProjectStartDate\",\"ProjectEndDate\"],\"offset\":0,\"limit\":10}​​​​​​​"

Curl output

plankton
  • 369
  • 5
  • 21

1 Answers1

1

The solution is simple, but easily missed. You need json=params See this question: How to POST JSON data with Python Requests?

response = requests.post("https://api.reporter.nih.gov/v2/projects/search", json=params)

Below is the entire code with the small change:

import requests

params = {
  "criteria":
    {
      "fiscal_years":[2019,2018]
    },
  "include_fields": [
    "ApplId","SubprojectId","FiscalYear","Organization", "ProjectNum","OrgCountry",
    "ProjectNumSplit","ContactPiName","AllText","FullStudySection",
    "ProjectStartDate","ProjectEndDate"
  ],
  "offset":0,
  "limit":25,
  "sort_field":"project_start_date",
  "sort_order":"desc"
}

response = requests.post("https://api.reporter.nih.gov/v2/projects/search", json=params)

print(response.status_code)
print(response.text)

The start of result looks like:

{"meta":{"search_id":null,"total":160216,"offset":0,"limit":25,"sort_field":"project_start_date","sort_order":"desc","sorted_by_relevance":false,"properties":{}},"results":[{"appl_id":10396858,"subproject_id":null,"fiscal_year":2018,"project_num":"7K01AG046366-06","organization":{"org_name":"UNIVERSITY OF CONNECTICUT SCH OF MED/DNT","city":null,"country":null,"org_city":"FARMINGTON","org_country":"UNITED STATES","org_state":"CT","org_state_name":null,"dept_type":"NEUROSCIENCES","fips_country_code":null,"org_duns":["022254226"],...
cannin
  • 2,735
  • 2
  • 25
  • 32