linux_driver
linux_driver

Reputation: 37

How to format a python" requests" that has a parameter

I am writing a python script to using an API to pull down JSON data using requests. A cleaned up snippet from the CURL that works is (with altered key and URL):

curl -G -H 'key: wwwxxxx' -H 'Content-Type: application/json' --data-urlencode 'from=2017-01-01 12:00:00' https://sampleurl/target1

How do I handle the "--data-urlencode 'from=2017-01-01 12:00:00'" ?

I think the code would be:

import requests

headers = {
    'key': 'wwwxxxx',
    'Content-Type': 'application/json',
}
url = 'https://sampleurl/target1'
data = requests.get(url, headers=headers)

Thanks in advance for any help!

UPDATE I tried the DATA suggested by zwer, but that threw a 404 error. They i just tried to add the parameter as a header pair and it worked!!!

So the code that works is:

  import requests

    headers = {
        'key': 'wwwxxxx',
        'Content-Type': 'application/json',
        'from' : '2017-01-01 12:00:00'
    }
    url = 'https://sampleurl/target1'
    data = requests.get(url, headers=headers)

Upvotes: 1

Views: 4092

Answers (1)

zwer
zwer

Reputation: 25769

Just use the data parameter:

import requests

headers = {
    'key': 'wwwxxxx',
    'Content-Type': 'application/json',
}
url = 'https://sampleurl/target1'
data = requests.get(url, headers=headers, data={"from": "2017-01-01 12:00:00"})

Upvotes: 2

Related Questions