Have a CURL request like that:
curl -X POST "https://page.com/login"
-H "accept: application/json" -H "Content-Type: application/json"
-d "{ \"username\": \"admin\", \"password\": \"pass\"}"
In Python I guess it should look like this:
import requests
headers = {
'Accept': 'application/json',
'Content-Type': 'application/json'
}
data = {'username': 'admin', 'password': 'pass'}
response = requests.post('https://page.com/login', headers=headers, data=data)
response
After this it gives me [502] error for bad gateway. What am I doing wrong with my python query and how it should be modified?
Try using:
requests.post(..., json=data)
When you use data= requests will send it form encoded, to actually put json in the body you have to use json=
Related
I am trying to POST a multipart/base64 xml file to portal using the following in python. How can i run it in Python?
curl -X POST -H 'Accept: application/xml' -H 'Content-Type: multipart/related; boundary=<boundary_value_that_you_have>; type=text/xml; start=<cXML_Invoice_content_id>' --data-binary #<filename>.xml https://<customer_name>.host.com/cxml/invoices
You can use this website
I got this code. Could you try it ?
import requests
headers = {
'Accept': 'application/xml',
'Content-Type': 'multipart/related; boundary=<boundary_value_that_you_have>; type=text/xml; start=<cXML_Invoice_content_id>',
}
data = open('filename.xml', 'rb').read()
response = requests.post('https://<customer_name>.host.com/cxml/invoices', headers=headers, data=data)
I'm trying to code the following cURL API request in Python:
curl -X POST 'https://api.livecoinwatch.com/coins/list' \
-H 'content-type: application/json' \
-H 'x-api-key: <YOUR_API_KEY>' \
-d '{"currency":"USD","sort":"rank","order":"ascending","offset":0,"limit":2,"meta":false}'
I tried solving it with guidance of another post, like this:
headers = {
'x-api-key': <YOUR_API_KEY>,
'content-type': 'application/json',
'host': https://api.livecoinwatch.com/coins/list
}
url = https://api.livecoinwatch.com/coins/list
data = '{"currency": "USD","sort": "rank","order": "ascending","offset": 0,"limit": 50,"meta": true}'
response = requests.post(url, data=json.dumps(data), headers=headers)
print (response)
Unfortunately I get a "bad request" error.
Can someone please help me where I go wrong?
Assuming you have your urls wrapped in quotes, you should try giving to the data function parameter a dictionary instead of a string as the requests documentation says: data – (optional) Dictionary, list of tuples, bytes, or file-like object to send in the body of the Request.
response = requests.post(url, data=json.loads(data), headers=headers)
I have the following Curl request:
curl -v --location --request POST 'http://127.0.0.1:8080/abc' \--header 'Content-Type: application/json' \--data-raw '{data}'
I tried using pycurl and requests command.
Also tried to put headers but it was of no use.
My tried code:
requests = "curl -v --location"
a = "http://127.0.0.1:8080/abc"
headers = {'Content-Type': 'application/json'}
r = requests.post(url=a, headers= headers , params=data)
Is this works?
https://curl.trillworks.com/
import requests
headers = {
'Content-Type': 'application/json',
}
data = '{data}'
response = requests.post('http://127.0.0.1:8080/abc',
headers=headers,
data=data)
So I wrote this script in curl to generate a token
token=$(curl -s -H "Accept: application/json" -H "Content-Type: application/json" --data '{"identifier": "...", "password": "..."}' "$HOST:$PORT/login" | ggrep -Po '"token":"(\K[^"]+)')
which works fine. However with the simplicity of Python 3 I'd like to perform the same task using requests. As I understand running
import requests, json
http = '...'
head = {'accept': 'application/json', 'Content-Type':'application/json'}
data = { 'username' : '...', 'password' : '...' }
r = requests.post(http, data=json.dumps(data), headers=head, verify=False)
print(r.text)
should return the same but I get the error
{"id":"3bca1f46-0577-40a9-8e09-7d68557ad88f","rafalError":"WrongJson","message":"DecodingFailure at .identifier: Attempt to decode value on failed cursor"}
with r.status_code returning 422.
I'm trying to grab some data from a website using API, but I'm having trouble converting the example curl command to python requests.
example curl command
curl -X POST "some_url" \
-H "accept: application/json" \
-H "Authorization: <accesstoken>" \
-d #- <<BODY
{}
BODY
My python requests that didn't work
headers = {
'Authorization': "Bearer {0}".format(access_token)
}
response = requests.request('GET', "some_url",
headers=headers, allow_redirects=False)
I get error code 400, can anyone help me figure out what was wrong?
The equivalent requests code for your curl should be:
import requests
headers = {
'accept': 'application/json',
'Authorization': '<accesstoken>',
}
data = "{} "
response = requests.post('http://some_url', headers=headers, data=data)
You can use https://curl.trillworks.com/ to convert your actual curl invocation (note that it won't handle heredocs, as in your example).
If you see different behavior between curl and your python code, dump the HTTP requests and compare:
Python requests - print entire http request (raw)?
How can I see the request headers made by curl when sending a request to the server?