Can someone please suggest the correct syntax for calling the below using python?
curl "https://sometest.api.token" \
-H "Content-Type: application/x-www-form-urlencoded" \
-d "grant_type=client_credentials&client_id={client_id}&client_secret={client_secret}"
My attempt:
import requests
import json
credentials='1111'
secret='2222'
url = 'https://sometest.api.token'
body = {'client_credentials':credentials,'client_secret':secret}
headers = {'Content-type': 'application/x-www-form-urlencoded'}
r = requests.post(url, data=json.dumps(body), headers=headers)
Due to documentation, if you want to send some form-encoded data, you simply pass a dictionary to the data argument.
So you have to try:
import requests
import json
credentials='1111'
secret='2222'
url = 'https://sometest.api.token'
body = {'client_credentials':credentials, 'client_secret':secret}
headers = {'Content-Type': 'application/x-www-form-urlencoded'}
r = requests.post(url, data=body, headers=headers)
And also your parameters in python code are different from parameters in curl, maybe you have to check it.
Id like to convert this curl command to python script
curl -v -H "Content-Type: application/json" -H "Authorization: Bearer MYTOKEN" https://www.zopim.com/api/v2/chats
I wrote the python script below but I get <Response [400]>and doesn't work.
import requests
url = "https://www.zopim.com/api/v2/chats"
access_token = "MYTOKEN"
response = requests.post(url,
headers={"Content-Type":"application/json;charset=UTF-8",
"Authorization": "Bearer {}".format(access_token)})
print(response)
Any advice will be appreciated.thanks.
You should be using requests.get instead of requests.post, since what you want is a GET request:
import requests
url = "https://www.zopim.com/api/v2/chats"
access_token = "MYTOKEN"
response = requests.get(url,
headers={"Content-Type":"application/json;charset=UTF-8",
"Authorization": "Bearer {}".format(access_token)})
print(response)
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)
I have a curl command I can run from my local machine that works, but transferring it to a python script is giving me difficulties. Here's the curl command that works:
curl -X PATCH "http://localhost:9999/pins/1" -H "Content-Type: application/json" -d "{"state": "on"}"
And here is what I have so far as a python request:
import requests
url = 'http://localhost:9999/pins/1'
payload = {'state':'on'}
head = {'Content-Type':'application/json'}
r = requests.patch(url, payload, headers=head)
But I am receiving a 400 response. Any direction?
Also, if I format it as
r = requests.patch(url, data=payload)
I get a 500 response code. Also should be noted: I can run a simple get request easily by running something like
r = requests.get(url)
import requests
url = 'http://localhost:9999/pins/1'
payload = {'state':'on'}
head = {'Content-Type':'application/json'}
r = requests.patch(url, json=payload)
change this r = requests.patch(url, data=payload) to r = requests.patch(url, json=payload)
Try the following to make sure that your payload is valid json.
import requests
import json
url = 'http://localhost:9999/pins/1'
payload = {'state':'on'}
head = {'Content-Type':'application/json'}
r = requests.patch(url, data=json.dumps(payload), headers=head)
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)