how to make a system curl call from terminal using python? - python

The call I want to make is this:
curl -i \
-H 'Harvest-Account-ID: 3012210627'\
-H 'Authorization: Bearer 184740.pt.VgNF7lMe1YDTjTH4_nfhm8NiH(qMyRI9kFS4BBvztnLM9P0HNgQvAHnlglnTA9q0wlmtrpoEONHVaT7phZAaNw'\
-H 'User-Agent: Harvest API Example' \
"https://api.harvestapp.com/api/v2/time_entries.json"
but I am not quite sure of how to do this. Any help will be appreciated.

It does not authenticate, because you did not send the JWT Token in HTTP headers:
import requests
headers = {
"Harvest-Account-ID": "380637",
"Authorization": "Bearer 184740.pt.VgNF7mMe1YDTjTH4_nfhm8NiT5IMyRI9kFS4AAvztnLM9P0HNgQvAHnlglnTA9X0wlmtrpoEONHVaT7phZAaNw",
"User-Agent": "Harvest API Example"
}
print (requests.get("https://api.harvestapp.com/api/v2/time_entries.json", headers=headers)
Returns:
<Response [200]>

Related

CURL POST with Python

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)

get 400 bad request after requesting GET in python

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)

Trouble converting curl commands to python requests

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?

Transforming CURL request to Python using requests library

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=

Convert a CURL to Python - HTTPS POST

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"

Categories