I am trying to make a 'put' method with curl everything is working fine and I got the JSON back:
curl -X PUT -d '[{"foo":"more_foo"}]' http://ip:6001/whatever?api_key=whatever
But for some reason when using the python requests
module as follow:
import requests
url = 'http://ip:6001/whatever?api_key=whatever'
a = requests.put(url, data={"foo":"more_foo"})
print(a.text)
print(a.status_code)
I get the following error:
500 Internal Server Error
Internal Server Error
The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.
NB: The server is up and running.
Curl Converter automatically generates valid Python code using the Python request library for all provided Curl HTTP headers and Curl data. Enter the Curl command, click Run to execute the command online and check the results. Click Generate Code and select Python to convert the Curl command to Python code.
To make a GET request using Curl, run the curl command followed by the target URL. Curl automatically selects the HTTP GET request method unless you use the -X, --request, or -d command-line option.
The reason why request might be blocked is that, for example in Python requests library, default user-agent is python-requests and websites understands that it's a bot and might block a request in order to protect the website from overload, if there's a lot of requests being sent.
In Python, cURL transfers requests and data to and from servers using PycURL. PycURL functions as an interface for the libcURL library within Python. Almost every programming language can use REST APIs to access an endpoint hosted on a web server.
The data should be dumped:
a = requests.put(url, data=json.dumps([{"foo":"more_foo"}]))
or you can use the json
key instead of data
:
a = requests.post(url, json=[{"foo":"more_foo"}])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With