Closed noahtren closed 4 years ago
Trying issuing your search requests as a POST — not documented (yet) but JSON passed as a parameter hardly ever works. You will also need to send your api key in the header as an x-api-key token or as a GET query parameter or as a username like in basic auth, i.e. https://API_KEY@api.nal.usda.gov/fdc https://API_KEY@api.nal.usda.gov/fdc …. etc. See https://api.data.gov/docs/api-key https://api.data.gov/docs/api-key HTH Gary
On Apr 4, 2019, at 4:48 PM, Noah Trenaman notifications@github.com wrote:
Hi all
Really excited to work with the new data available from FDC, the Foundational Foods Dataset looks especially interesting. I have been working on a Python package recently that interfaces with the Standard Reference Database and would like to try extending its access to Foundational Foods.
The FDC website gives examples for how to implement the curl command to interface with the API, but I am having trouble transcribing this to Python requests.
For example, the follow request gets an error response:
params = {"generalSearchInput":"almond", "api_key":API_KEY} response = (requests.get(url="https://api.nal.usda.gov/fdc/v1/search", params=params).json) This returns
{'timestamp': '2019-04-04T20:45:15.257+0000', 'status': 400, 'error': 'Bad Request', 'message': 'Failed to convert value of type \'java.lang.String\' to required type \'java.lang.Long\'; nested exception is java.lang.NumberFormatException: For input string: "search"', 'path': '/portal-data/api/v1/search'} — You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/USDA/USDA-APIs/issues/64, or mute the thread https://github.com/notifications/unsubscribe-auth/ABHe9UbRNe2bJc83pyvh5O_AoB_C9p3Mks5vdmUPgaJpZM4cdrjE.
Hi, thanks for the help! I've set up the search request as a POST and have assigned the api key as a header. I am now getting a different error which I'm suspecting may be due to JSON not working?
target_url = "https://api.nal.usda.gov/fdc/v1/search"
params = {'generalSearchInput':'almond'}
headers = {"x-api-key":API_KEY}
response = requests.post(target_url, headers=headers, data=params).json()
{'timestamp': '2019-04-05T15:09:52.575+0000', 'status': 415, 'error': 'Unsupported Media Type', 'message': "Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported", 'path': '/portal-data/api/v1/search'}
I’m not a python programmer, you might need to add a “Content-type: application/json” header. Gary
On Apr 5, 2019, at 11:17 AM, Noah Trenaman notifications@github.com wrote:
Hi, thanks for the help! I've set up the search request as a POST and have assigned the api key as a header. I am now getting a different error which I'm suspecting may be due to JSON not working?
target_url = "https://api.nal.usda.gov/fdc/v1/search" params = {'generalSearchInput':'almond'} headers = {"x-api-key":API_KEY} response = requests.post(target_url, headers=headers, data=params).json() {'timestamp': '2019-04-05T15:09:52.575+0000', 'status': 415, 'error': 'Unsupported Media Type', 'message': "Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported", 'path': '/portal-data/api/v1/search'} — You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/USDA/USDA-APIs/issues/64#issuecomment-480314706, or mute the thread https://github.com/notifications/unsubscribe-auth/ABHe9b5UuJ5R2Bz74PRfu-BliLKxVVmMks5vd2kOgaJpZM4cdrjE.
I would close this issue.
Revisiting this problem after a long time and found the solution. Firstly, it needs to be a POST request instead of GET as @littlebunch mentions. The extra step is to pass the main body of the request with the json
parameter, instead of passing everything to params
.
params = {'api_key': key}
data = {'generalSearchInput': 'chia'}
response = requests.post(
r'https://api.nal.usda.gov/fdc/v1/search',
params=params,
json=data
)
print(response.json())
Hi all,
Really excited to work with the new data available from FDC, the Foundational Foods Dataset looks especially interesting. I have been working on a Python package recently that interfaces with the Standard Reference Database and would like to try extending its access to Foundational Foods.
The FDC website gives examples for how to implement the
curl
command to interface with the API, but I am having trouble transcribing this to Python requests.For example, the follow request gets an error response:
This returns