0

I am having some trouble running the Freedom of information act API in python. I am sure it is related to how I am implementing my API key but I am uncertain as to where I am dropping the ball. Any help is greatly appreciated.

import requests

apikey= ''

api_base_url = f"https://api.foia.gov/api/webform/submit"
endpoint = f"{api_base_url}{apikey}"
r = requests.get(endpoint)

print(r.status_code)
print(r.text)

there error I receive is requests.exceptions.InvalidSchema: No connection adapters were found for this website. Thanks again

Braiam
  • 1
  • 11
  • 47
  • 78
  • no it doesn't. Thank you for correcting that error. I still get a 403 error after removing it – Meredith Jackson Sep 19 '20 at 03:01
  • According to [the documentation](https://www.foia.gov/developer/#submission), you should be providing your API key via the `X-API-Key` header, not as part of the request URL. – larsks Sep 19 '20 at 03:07
  • Also, I'm not sure you can make a `GET` request against that endpoint. But I haven't read the spec closely enough to be sure on that point. – larsks Sep 19 '20 at 03:08
  • 1
    @larsks - There are examples of GET requests in the documentation ... – Stephen C Sep 19 '20 at 03:13

1 Answers1

1

According to the documentation, the API requires the API key to be passed as a request header parameter ("X-API-Key"). Your python code appears to be simply concatenating the API key and the URL.

The following Q&A explains how to set a request header using requests.

It would be something like this:

import requests

apikey= ...
api_base_url = ...
r = requests.get(api_base_url, 
                 headers={"X-API-Key": apikey})

print(r.status_code)
print(r.text)

Note that the documentation for the FOIA site explains what you need to do to submit a FIOA request form. It is significantly different to what your Python code is apparently trying to do. I would advise you to read the documentation. Also read the manual entry for the "curl" command so that you understand the requests that the examples show.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216