JoeMcG
JoeMcG

Reputation: 129

formatting JSON with python requests package for post command...?jsonRequest parameter in URL

I am trying to submit a POST request to the USGS EarthExplorer inventory API. This starts with a simple log-in. They have a test page:

https://earthexplorer.usgs.gov/inventory/documentation/test

which allows you to see some formatting examples. For the log-in example, I was able to extract the URL submitted on the button press as (user and pw have been changed):

https://earthexplorer.usgs.gov/inventory/json/v/1.4.0/login?jsonRequest=%7B%22username%22%3A%22user%22%2C%22password%22%3A%22pw%22%2C%22catalogId%22%3A%22EE%22%7D

However, I cannot seem to figure out how to format this in Python using the requests library. I am open to others, but am using requests for now. I have tried creating a dictionary as:

creds = {"username": "user",
  "password": "pw",
  "authType": "",
  "catalogId": "EE"}

payload = json.dumps(creds)

When I call requests.post(url, json=payload) I am told my username parameter does not exist. I have tried other keywords like data and params as well.

I have noticed the jsonRequest parameter in the successful URL, so I tried creating a dictionary with that in there as:

creds2={"jsonRequest": [{"username": "user",
                    "password": "pw",
                    "authType": "",
                    "catalogId": "EE"}]} 

but this doesn't work either.

Any suggestions? Thanks!

Upvotes: 1

Views: 149

Answers (1)

t.m.adam
t.m.adam

Reputation: 15376

You'll have to send a GET request and pass the creds in the query string using the params argument.

creds = {
    "username": "user",
    "password": "pw",
    "authType": "",
    "catalogId": "EE"
}
url = 'https://earthexplorer.usgs.gov/inventory/json/v/1.4.0/login'
r = requests.get(url, params={'jsonRequest':json.dumps(creds)})
print(r.json())

Upvotes: 1

Related Questions