简体   繁体   中英

curl works but not python requests.get()

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'])

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM