I'm facing an issue with the code below:
!curl -X POST \
-H 'Content-Type':'application/json' \
-d '{"data":[[4]]}' \
http://0.0.0.0/score
How can I convert this code into a Python function or using Postman?
I'm facing an issue with the code below:
!curl -X POST \
-H 'Content-Type':'application/json' \
-d '{"data":[[4]]}' \
http://0.0.0.0/score
How can I convert this code into a Python function or using Postman?
import requests
payload = {
"data": [[4]]
}
headers = {
'Content-Type': "application/json",
}
server_url = 'http://0.0.0.0/score'
requests.post(server_url, json = payload, headers = headers)
should be roughly equivalent to your curl command.
Otherwise, to "translate" curl into Python commands you could use tools like https://curl.trillworks.com/#python.
Postman has a convenient "import" tool to import curl commands like yours (pasting your command as raw text).
The result can also be "exported" into Python code using Postman.
Shortest equivalent (with requests lib) will look like this:
import requests # pip install requests
r = requests.post("http://0.0.0.0/score", json={"data":[[4]]})
requests will automatically set appropriate Content-Type header for this request.
Note that there will still be some differences in request headers because curl and requests always set their own set of headers implicitly.
Your curl command will send this set of headers:
"Accept": "*/*",
"Content-Length": "8", # not the actual content length
"Content-Type": "application/json",
"Host": "httpbin.org", # for testing purposes
"User-Agent": "curl/7.47.0"
And requests headers will look like that:
"Accept-Encoding": "gzip, deflate",
"Host": "httpbin.org",
"User-Agent": "python-requests/2.22.0",
"Content-Length": "8",
"Accept": "*/*",
"Content-Type": "application/json"
So you can manually specify User-Agent header in headers= keyword argument if needed.
But compression will still be used.