The following code only works in curl. It would be nice if you could tell me why it isnt working in Python using Requests
curl 'http://cdcnepal.com/Modules/HOmeMoviesLists/WebService2.asmx/GetShowsByDate' \
-H 'Content-Type: application/json; charset=UTF-8' \
-d '{"portalId":"1","showDate":"26/05/2014","flag":0,"size":9}'
However in Python with the following code
import requests
import json
url = """http://cdcnepal.com/Modules/HOmeMoviesLists/WebService2.asmx/GetShowsByDate"""
headers = {"content-type":["application/json", "charset=UTF-8"]}
payload = {"portalId":"1","showDate":"26/05/2014","flag":0,"size":9}
r = requests.get(url, headers=headers, data=payload)
print r.text
Originally the curl request had other content, below, however I realised I could remove several. I'm not sure that is causing the error because the curl request is working. I'm not getting the same response from both the code.
This might be useful. A Curl Requests extracted from Chrome Dev Tools
curl 'http://cdcnepal.com/Modules/HOmeMoviesLists/WebService2.asmx/GetShowsByDate'
-H 'Cookie: OriginalReferrer=https://www.google.com/;
OriginalURL=http://cdcnepal.com/;
ASP.NET_SessionId=i5lbnql5hpp0wm1ywyqbywtj;
VisitCount=4'
-H 'Origin: http://cdcnepal.com'
-H 'Accept-Encoding: gzip,deflate,sdch'
-H 'Accept-Language: en-US,en;q=0.8,hi;q=0.6'
-H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3)
AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.114 Safari/537.36'
-H 'Content-Type: application/json; charset=UTF-8'
-H 'Accept: application/json, text/javascript, */*; q=0.01'
-H 'Referer:http://cdcnepal.com/Home.aspx'
-H 'X-Requested-With: XMLHttpRequest' -H 'Connection: keep-alive'
-H 'DNT: 1'
--data-binary '{"portalId":"1","showDate":"27/05/2014","flag":0,"size":9}' --compressed
The curl -d switch sends a POST request, but you are using requests.get() instead, sending a GET request (whose body is ignored).
Make it a POST instead, by using request.post():
import requests
import json
url = "http://cdcnepal.com/Modules/HOmeMoviesLists/WebService2.asmx/GetShowsByDate"
headers = {"content-type": "application/json; charset=UTF-8"}
payload = {"portalId":"1","showDate":"26/05/2014","flag":0,"size":9}
r = requests.post(url, headers=headers, data=json.dumps(payload))
print r.text
You also need to:
not use a list for the content-type header, there is no support for paramaters being specified separately.
Encode your JSON data to a JSON string; requests doesn't do this for you. Instead, a dictionary passed to data is encoded as application/x-www-form-urlencoded data instead.
You can compare the curl command with requests more easily using http://httpbin.org/post:
$ curl http://httpbin.org/post \
> -H 'Content-Type: application/json; charset=UTF-8' \
> -d '{"portalId":"1","showDate":"26/05/2014","flag":0,"size":9}'
{
"args": {},
"data": "{\"portalId\":\"1\",\"showDate\":\"26/05/2014\",\"flag\":0,\"size\":9}",
"files": {},
"form": {},
"headers": {
"Accept": "*/*",
"Connection": "close",
"Content-Length": "58",
"Content-Type": "application/json; charset=UTF-8",
"Host": "httpbin.org",
"User-Agent": "curl/7.30.0",
"X-Request-Id": "78d7bb7d-e29b-482b-908a-48d2395a050f"
},
"json": {
"flag": 0,
"portalId": "1",
"showDate": "26/05/2014",
"size": 9
},
"origin": "84.92.98.170",
"url": "http://httpbin.org/post"
}
and
>>> import requests
>>> import json
>>> from pprint import pprint
>>> url = 'http://httpbin.org/post'
>>> headers = {"content-type":"application/json; charset=UTF-8"}
>>> payload = {"portalId":"1","showDate":"26/05/2014","flag":0,"size":9}
>>> r = requests.post(url, headers=headers, data=json.dumps(payload))
>>> pprint(r.json())
{u'args': {},
u'data': u'{"portalId": "1", "flag": 0, "size": 9, "showDate": "26/05/2014"}',
u'files': {},
u'form': {},
u'headers': {u'Accept': u'*/*',
u'Accept-Encoding': u'gzip, deflate, compress',
u'Connection': u'close',
u'Content-Length': u'65',
u'Content-Type': u'application/json; charset=UTF-8',
u'Host': u'httpbin.org',
u'User-Agent': u'python-requests/2.2.1 CPython/2.7.6 Darwin/13.1.0',
u'X-Request-Id': u'06d6b542-c279-4898-8701-2c0d502aa36e'},
u'json': {u'flag': 0,
u'portalId': u'1',
u'showDate': u'26/05/2014',
u'size': 9},
u'origin': u'84.92.98.170',
u'url': u'http://httpbin.org/post'}
Both cases show the same json dictionary being returned.
If you are using requests version 2.4.2 or newer, you can also leave the JSON encoding to the library; it'll set the correct Content-Type header too, if you pass in the data to send as the json keyword argument:
import requests
url = "http://cdcnepal.com/Modules/HOmeMoviesLists/WebService2.asmx/GetShowsByDate"
payload = {"portalId":"1","showDate":"26/05/2014","flag":0,"size":9}
r = requests.post(url, json=payload)
print r.text
Related
I have this request using a curl command and I want to translate it to python using the requests library
curl -X POST https://endpoint/prod/api/Translations/start \
-H 'Authorization: Bearer <accessToken>' \
-H 'Content-Type: application/json' \
-d '{ "text": ["first segment to translate.", "second segment to translate."], "sourceLanguageCode": "en", "targetLanguageCode": "de", "model": "general", "useCase": "testing"}'
You can use requests library.
The following curl:
curl -X POST "https://www.magical-website.com/api/v2/token/refresh/" \
-H 'accept: application/json' \
-H 'Content-Type: application/json' \
-d '{
"refresh": "$REFRESH_TOKEN"
}'
I wrote in python the following way:
import requests
def get_new_token():
url = 'https://www.magical-website.com/api/v2/token/refresh/'
token = constants.THE_TOKEN
payload = f'{{ "refresh": "{token}" }}'
headers = {"accept": "application/json", "Content-Type": "application/json"}
print("Token handling ....")
r = requests.post(url, data=payload, headers=headers)
print(f"Token status: {r.status_code}")
return r.json()['access']
You can try this one.
import requests
url = "https://endpoint/prod/api/Translations/start"
payload = {
"text": ...,
"sourceLanguageCode": ...,
...
}
headers = { "Authorization": "Bearer ...", "Content-Type": "application/json" }
res = requests.post(url, data = payload, headers = headers)
print(res.status_code)
print(res.text)
I'm trying to turn this cURL request into a Python code. I want to eventually be able to save this to a CSV file but I need to get connected first.
curl --compressed -H 'Accept: application/json' -H 'X-Api-Key: 123abc' 'https://us.market-api.kaiko.io/v2/data/trades.v1/exchanges/cbse/spot/btc-usd/aggregations/count_ohlcv_vwap?interval=1h'
I started with this:
import requests
import json
key='api-key'
url = 'https://us.market-api.kaiko.io/v2/data/trades.v1/exchanges/'
s = requests.Session()
s.auth = (key)
headers = {
*not sure how to do this*
}
r = requests.get(url, headers=headers)
The docs say this needs to be in the header:
Accept: application/json
Accept-Encoding: gzip:
How do I include the API key? how do I save the data once its returned?
X-Api-Key would be a request header, so you can include it in your headers variable, like this:
headers = {
"X-Api-Key": key,
"Accept": "application/json",
"Accept-Encoding": "gzip"
}
(took the others ones from your current curl request)
You can get the data by using r.text, like this:
print(r.text)
Your code should look like this:
import requests
import json
key='api-key'
url = 'https://us.market-api.kaiko.io/v2/data/trades.v1/exchanges/'
headers = {
"X-Api-Key": key,
"Accept": "application/json",
"Accept-Encoding": "gzip"
}
r = requests.get(url, headers=headers)
print(r.text)
If you want to get a json object instead, you can use r.json()
I am trying to get the sandbox of HSBC's Open Banking to work. The curl command from their documentation gives me a access token only when I disable verification with -k. Note that I downloaded the xyz.der and server.key from my HSBC developer dashboard.
curl -v -k -X POST \
--cert hsbc/qwac_PSP_PI,PSP_AS,PSP_IC,PSP_AI_27_10_2020.der \
--cert-type DER \
--key hsbc/server.key \
-H "Content-Type: application/x-www-form-urlencoded" \
-H "Accept: application/json" \
-H "x-fapi-financial-id: test" \
-H "Cache-Control: no-cache" \
-d 'grant_type=client_credentials&scope=accounts&client_assertion_type=urn:ietf:params:oauth:client-assertion-type:jwt-bearer&client_assertion=xyz' \
"https://sandbox.hsbc.com/psd2/obie/v3.1/as/token.oauth2"
Since this is working I am trying to do the same with requests but am struggling in how to use the certificates. I know requests supports the cert keyword but it seems I would need to add the other parameters too. Is there a way I can specifics the certificate type and corresponding key?
import requests
headers = {
"Content-Type": "application/x-www-form-urlencoded",
"Accept": "application/json",
"x-fapi-financial-id": "test",
"Cache-Control": "no-cache",
}
params = {
"grant_type": "client_credentials",
"scope": "accounts",
"client_assertion_type": "urn:ietf:params:oauth:client-assertion-type:jwt-bearer",
"client_assertion": "xyz",
}
url = "https://sandbox.hsbc.com/psd2/obie/v3.1/as/token.oauth2"
requests.post(url=url,
headers=headers,
params=params,
cert="hsbc/qwac_PSP_PI,PSP_AS,PSP_IC,PSP_AI_27_10_2020.der",
verify=False).json()
Thanks to #KlausD. this works now after converting the .der to .pem with openssl:
openssl x509 -inform der -in qwac_xyz.der -out qwac_xyz.pem
import requests
headers = {
"Content-Type": "application/x-www-form-urlencoded",
"Accept": "application/json",
"x-fapi-financial-id": "test",
"Cache-Control": "no-cache",
}
params = {
"grant_type": "client_credentials",
"scope": "accounts",
"client_assertion_type": "urn:ietf:params:oauth:client-assertion-type:jwt-bearer",
"client_assertion": "xyz",
}
url = "https://sandbox.hsbc.com/psd2/obie/v3.1/as/token.oauth2"
requests.post(url=url,
headers=headers,
params=params,
cert=("hsbc/qwac_xyz.pem", "hsbc/server.key"),
verify=False).json()
I would like to use crushftp api to set password for a user.
I'm using update user API
https://www..com/crush9wiki/Wiki.jsp?page=API
It has something like in the Curl
--data-urlencode
> 'user=<?xml+version="1.0"+encoding="UTF-8"?>+<user+type="properties"><password>thisismypass</password></user>'
curl -d command=setUserItem -d data_action=update -d xmlItem=user -d serverGroup=MainUsers -d username=curl_user --data-urlencode 'user=<?xml+version="1.0"+encoding="UTF-8"?>+<user+type="properties"><password>thisismypass</password></user>' http://crushadmin:pass#127.0.0.1:8080/
I try to convert:
import requests
url = "http://crushadmin:pass#127.0.0.1:8080/"
payload = "command=setUserItem&data_action=update&xmlItem=user&serverGroup=MainUsers&username=curl_user"
headers = {
'cache-control': "no-cache",
'Postman-Token': "9b1c470d-889a-4647-a0f1-4b2a5c00bf68"
}
response = requests.request("GET", url, data=payload, headers=headers)
print(response.text)
However, i don't know how to put xlm including password needed to change
user=+thisismypass
import requests
url = 'http://crushadmin:pass#127.0.0.1:8080/'
payload = {
'command': 'setUserItem',
'data_action': 'update',
'xx': 'xx'}
headers = {
'cache-control': 'no-cache',
'Postman-Token': '9b1c470d-889a-4647-a0f1-4b2a5c00bf68'}
response = requests.get(url=url, params=payload, headers=headers)
print(response)
I'm trying to POST some data to an HTTPS server. It requires a very particular set of headers. I'm able to complete the request, but I'm unable to do so in Python.
The curl:
curl -i
-H "Authorization: Basic a2V5OnNlY3JldA=="
-H "Content-Type: application/x-www-form-urlencoded"
-H "Content-Length: 99"
-H "User-Agent: Dalvik/2.1.0 (Linux; U; Android 5.1.1; Nexus 6 Build/LMY48Y)"
-H "Host: test.example.com"
-H "Connection: Keep-Alive"
-H "Content-type: application/json"
-d "grant_type=password&username=me%40example.com&password=abcd*1234&scope=scope1_services+scope1_data"
"https://test.example.com/login/get/token/"
The Python is
import httplib, urllib
host = "test.example.com"
url = "/login/get/token/"
params = urllib.urlencode({"grant_type":"password", "username":"me#example.com", "password":"abcd*1234", "scope":"scope1_services+scope1_data" })
headers = {"Authorization": "Basic a2V5OnNlY3JldA==", "Content-type": "application/x-www-form-urlencoded", "Content-Length":"99", "User-Agent":"Dalvik/2.1.0 (Linux; U; Android 5.1.1; Nexus 6 Build/LMY48Y)", "Host":host, "Connection": "Keep-Alive", "Content-type":"application/json"}
conn = httplib.HTTPSConnection(host)
conn.request("POST", url, params, headers)
response = conn.getresponse()
print response.status, response.reason
I just end up with 400 Bad Request and the error message
{
"error":"unsupported_grant_type",
"error_description":"The authorization grant type is not supported by the authorization server."
}
As far as I can tell, everything should be the same.
I've tried manually encoding the POST payload as params="grant_type=password&user... but I still get the same error.
Any idea what incredibly obvious thing I'm missing?
I think it is actually Content-Type not Content-type. Watch out the capital T there.
Also, I think you can remove these safely from the header:
"Content-Length":"99",
"Host":host,
"Connection": "Keep-Alive",
And, are you sure you need this? You are not posting any json here!
"Content-type":"application/json"