curl works but not python requests.get()

Viewed 1475

I've found some similar questions on stackoverflow (1,2,3), but none of them solved my problem.

The URL is something like "https://example.com/api.php?foo=bar". Using the following command will get a json string:

curl "https://example.com/api.php?foo=bar"

while using the following code returns a 502 response (bad gate way):

import requests
requests.get("https://example.com/api.php?foo=bar")

Currently I'm using the following code as a workaround, but I'd like to know why requests.get() doesn't work.

import subprocess
subprocess.check_output(['curl','-s','https://example.com/api.php?foo=bar'])
1 Answers

Try passing the parameter foo with the value bar using the params keyword instead of as part of the URL:

payload = {"foo": "bar"}
requests.get("https://www.example.com/api.php", params=payload)

documentation page

Related