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.
import requests
import json
import numpy as np
payload = {'query':json.dumps({
"per_page":12,
"sort":None,
"scroll_id":None,
"session_id":None,
"q":"diapers",
"shingle_active":False,
"location":"110005",
"types":["allopathy","brand","sku","udp"],
"country":"",
"is_query_suggestion_applicable":False,
"debug":False,
"filters":None,
"source_fields":["count"],
"query_filters":None,
"is_all":True
})}
headers = {'Content-Type': 'application/json'}
url = 'https://kjhghfjdslsls'
r = requests.post(url, params=payload, headers=headers)
print(r.text)
This gives error :
{"errors":[{"message":"Parameter per_page is required"}],"is_success":false,"status_code":400}
Though when using the below curl request it is working fine & returning required output-->
curl --location --request POST 'http://khjfhdksl' \
--header 'Content-Type: application/json' \
--header 'Content-Type: application/json' \
--data-raw '{
"sort":null,
"per_page":12,
"scroll_id":null,
"session_id":null,
"q":"diapers",
"shingle_active":false,
"location":"110005",
"types":["allopathy","brand","sku","udp"],
"country":"",
"is_query_suggestion_applicable":false,
"debug":false,
"filters":null,
"source_fields":["count"],
"query_filters":null,
"is_all":true
}'
You have a top-level key query in the payload for the python version. And you are passing the payload as params (query params)
Try removing that:
import requests
import json
import numpy as np
payload = json.dumps({
"per_page":12,
"sort":None,
"scroll_id":None,
"session_id":None,
"q":"diapers",
"shingle_active":False,
"location":"110005",
"types":["allopathy","brand","sku","udp"],
"country":"",
"is_query_suggestion_applicable":False,
"debug":False,
"filters":None,
"facets":[{"field":"sku.brand.raw","name":"brand","type":"facet","range":None},{"field":"product_form","name":"product_form","type":"facet","range":None},{"field":"rx_required","name":"rx_required","type":"facet","range":None},{"field":"uses","name":"uses","type":"facet","range":None},{"field":"age","name":"age","type":"facet","range":None},{"field":"recommended","name":"recommended","type":"facet","range":None}],
"source_fields":["count"],
"query_filters":None,
"is_all":True
})
headers = {'Content-Type': 'application/json'}
url = 'https://search-stag123api.example.com/search-new/search/v1/search/lambda'
r = requests.post(url, data=payload, headers=headers)
print(r.text)
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?
I am trying to convert a curl request to python request, but i have problem to convert -u .
curl -X POST \
-u "apikey:yourKey" \
--header "Content-Type: audio/wav" \
--data-binary "#path" \
"https://stream-fra.watsonplatform.net/speech-to-text/api/v1/recognize?model=de-DE_BroadbandModel
My solution:
import requests
data = "path"
url = 'https://stream-fra.watsonplatform.net/speech-to-text/api/v1/recognize?model=de-DE_BroadbandModel'
#payload = open("request.json")
headers = {'content-type': 'audio/wav', 'username': "apikey=yourkey" }
r = requests.post(url, headers=headers, data=data)
Edit:
import requests
data = "path"
url = 'https://stream-fra.watsonplatform.net/speech-to-text/api/v1/recognize?model=de-DE_BroadbandModel'
#payload = open("request.json")
headers = {'Content-Type': 'audio/wav'}
#r = requests.post(url, headers=headers, data=data)
print requests.post(url, verify=False, headers=headers, data=data, auth=('apikey', "key"))
now i get
Response [400]
(the curl cmd is working)
Try this
requests.post(url, headers=headers, data=data, auth=(apiKey, yourApiKey))
-u is short for --user which is used for server authentication see here, also look into Basic authentication for requests library.
Edit: You need to read the file (specified in --data-binary "#path") first before passing it in requests.post. I hope this link helps