I am trying to use the toggl api.
I use Requests instead of Urllib2 for doing my GETs en POSTS. But i am stuck.
payload = {
"project":{
"name":"Another Project",
"billable":False,
"workspace":{
"Name":"jorrebor's workspace",
"id":213272
},
"automatically_calculate_estimated_workhours":False
}
}
url = "https://www.toggl.com/api/v6/projects.json"
r = requests.post(url, data=json.dumps(payload), auth=HTTPBasicAuth('j_xxxxx#gmail.com', 'mypassword'))
Authentication seems to be fine, but the payload format probably isn't.
a curl command with the same parameters:
curl -v -u heremytoken:api_token -H "Content-type: application/json" -d "{\"project\":{\"billable\":true,\"workspace\":{\"id\":213272},\"name\":\"Another project\",\"automatically_calculate_estimated_workhours\":false}}" -X POST https://www.toggl.com/api/v6/projects.json
does work fine.
What wrong with my payload? The response is get is:
["Name can't be blank","Workspace can't be blank"]
which leads me to conclude that the authentication works but toggl cannot read my json object.
Seems like you should try setting the header to a JSON application, rather than the default format, and send a JSON object instead of the Python dict. Check it out here:
payload = {"project":{"name":"Another Project",
"billable":False,
"workspace":{"Name":"jorrebor's workspace",
"id":213272},
"automatically_calculate_estimated_workhours":False
} }
parameters_json = json.dumps(payload)
headers = {'Content-Type': 'application/json')
r = client.post(url, data=parameters_json, headers=headers)
which should allow the site to read the json object just fine.
Related
I am trying to convert the following cULR command into Python:
curl --location --request POST 'api.datamyne.com/frontend/REST/application' \
--header 'Content-Type: application/json' \
--data-raw '{
"token": "boKnofR06mzldz5eL00ARwa3B9winzpn",
"idApp":44
}'
I have the following Python code, but does not seem to work. Any idea how to include the raw data into into the request?
import requests
headers = {
'Content-Type': 'application/json',
'token': 'boKnofR06mzldz5eL00ARwa3B9winzpn',
'idApp': '44'
}
response = requests.get('http://api.datamyne.com/frontend/REST/applications', headers=headers)
print(response.content)
So in your python example you have called request.get().
If you call request.post() it will instead become a POST request.
as for adding it to the body, you could try a body variable:
data = {
'token': 'boKnofR06mzldz5eL00ARwa3B9winzpn',
'idApp': '44'
}
response = requests.post('http://api.datamyne.com/frontend/REST/applications',
headers=headers,
data = data)
Update:
This still fails because of an incorrectly formatted body.
to fix this i imported the json package and wrote:
#load string into a json object
data = json.loads('{"token": "boKnofR06mzldz5eL00ARwa3B9winzpn", "idApp": 44 }')
# json.dumps outputs a json object to a string.
response = requests.post('https://api.datamyne.com/frontend/REST/application', headers=headers, data=json.dumps(data))
I am trying to translate a specific curl method into Python's requests module to upload a file to to an api. My standard method that works for non-file requests looks like this:
import requests
requestObject = requests.Session()
standard_headers = {header1:headerValue1,header2:headerValue2}
payload = {key1:value1,key2:value2}
url = 'https://myUrl.com/apiCall'
requestObject.post(url,headers=standard_headers, json=payload)
This works for non-file requests that I need to make to the API. However for file uploads, the API documentation shows a method using curl:
curl -XPOST -H 'header1' -H 'header2 'https://myUrl.com/apiCall' \
-F 'attachment=#/path/to/my/file' \
-F '_json=<-;type=application/json' << _EOF_
{
"key1":"keyValue1",
"key2":"keyValue2"
}
_EOF_
I tested the curl command and it works successfully.
My question is how do I translate that curl method using the << _EOF_ method in Python requests. One idea I had was simply to use the 'files' option in the requests module:
requestObject = requests.Session()
standard_headers = {header1:headerValue1,header2:headerValue2}
payload = {key1:keyValue1,key2:keyValue2}
url = 'https://myUrl.com/apiCall'
file_to_upload = {'filename': open('/path/to/my/file', 'rb')}
requestObject.post(url,headers=standard_headers, files=file_to_upload, json=payload)
But that does not seem to work as the necessary json parameters (the values in payload) do not appear to get passed to the file upload
I also tried specifying the json parameters directly into the file_to_upload variable:
requestObject = requests.Session()
standard_headers = {header1:headerValue1,header2:headerValue2}
url = 'https://myUrl.com/apiCall'
file_to_upload = {'attachment': open('/path/to/my/file', 'rb'),'{"key1":"keyValue1","key2":"keyValue2"}'}
requestObject.post(url,headers=standard_headers, files=file_to_upload)
Similar result, it seems as though I am not passing the necessary json values correctly. I tried a few other ways but I am overlooking something. Any insight into how I should structure my request is appreciated.
Ok I managed to get it to work and posting for anyone who might need help in the future.
The trick was to include the _json key in the data field. My code ended up looking like so:
import requests
requestObject = requests.Session()
standard_headers = {header1:headerValue1,header2:headerValue2}
json_fields = json.dumps({
"key1": "key1Value",
"key2": "key2Value"
})
payload = {"_json":json_fields)
file = {"attachment": /path/to/my/file}
url = 'https://myUrl.com/apiCall'
requestObject.post(url,headers=standard_headers, files=file, data=payload)
Hope that helps some future person.
curl --request POST -H "Content-Type: application/octet-stream" --data-binary "#/C:\\Users\\U6068366\\Downloads\\Koala.jpg" https://c6y09pww43.execute-api.us-east-1.amazonaws.com/p
--
App_Url = "https://p7a0km3l6k.execute-api.us-east-1.amazonaws.com/preprod/v1/images/trademark/metadata/providerPartition/{providerPartition}/providerPartitionId/{providerPartitionId}"
# f = open('C://Users//UX016491//PycharmProjects//DSSApi//data1.json')
# requests_json = json.loads(f.read())
files = {'media' : open('C:\\Users\\UX016491\\Desktop\\images\\image123.jpg','rb') }
response = requests.request("POST", App_Url, files = files, headers={"content-type": 'application/octet-stream'})
print(response)
if __name__ == '__main__':
test_createimage_data()
EDIT: I added url from python example because url in curl was incomplete. But it still need two values providerPartition and providerPartitionId
On https://curl.trillworks.com/ you can convert curl to python code. And mostly it works.
Here code from this page. But I can't test it.
import requests
# incompletet url from curl
#url = 'https://c6y09pww43.execute-api.us-east-1.amazonaws.com/p'
providerPartition = '??'
providerPartitionId = '??'
url = f'https://p7a0km3l6k.execute-api.us-east-1.amazonaws.com/preprod/v1/images/trademark/metadata/providerPartition/{providerPartition}/providerPartitionId/{providerPartitionId}'
headers = {
'Content-Type': 'application/octet-stream',
}
data = open('C:\\Users\\U6068366\\Downloads\\Koala.jpg', 'rb').read()
response = requests.post(url, headers=headers, data=data)
print(response.text)
You can also test both with url https://httpbin.org/post and it will send back what it get from you. And you can compare results from both requests. I tested curl and python code and I go the same information so they should give the same effect.
Hi
Following curl command works and am trying to post the same (with a different JSON data )using requests.post and running into below error shown,any guidance on what is wrong?
curl -vk "https://splunk-hec.company.com:8088/services/collector" -H "Authorization: {token id }" -d '{"sourcetype": "source","index":"indexname", "event": {"a": "value1", "b": ["value1_1", "value1_2"]}}'
PYTHON CODE:-
_raw = {
"Total_radar_count":"999",
"Analyze":{
"Screen":{"count":110,"radar_link":"change://problem/50411162&42639456&44776863&43703933"},
"Investigate":{"count":065,"radar_link":"change://problem/50411162&42639456&44776863&43703933"},
"Review":{"count":106,"radar_link":"change://problem/50411162&42639456&44776863&43703933"}
},
"timestamp": int(time.time()) # Can also use datetime.datetime.now().isoformat()
}
url = 'https://splunk-hec.company.com:8088/services/collector?sourcetype=source?index=indexname'
json = _raw
auth_token = 'token id'
head = {'Authorization': auth_token}
response = requests.post(url, json=json, headers=head)
print(response)
print (response.reason)
print(response.json())
ERROR:-
<Response [400]>
Bad Request
{u'text': u'No data', u'code': 5}
400 (Bad Request) can be many things; see the doc at https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/400 . Next debugging step is to get out wireshark and look at the difference between the two requests. Obviously your data is different between the two, so it could be rejecting it just based on what it expects.
Also check the server-side log. Chances are the real error is in there.
Try using
requests.post(url, headers=head, data=json.dumps(json))
You also need to import json package but don't wory it is a built in package
This question is quite old. However, I thought to share this as I think it may helpful others. Just try adding "event": for you json post data.
response = requests.post(url, json={"event": json}, headers=headers)
I am using requests to make a POST request to create a user. The request succeeds with 201 created when I use curl, however fails with a 500 response when I use requests. My curl command is
curl --user administrator:password -H "Content-Type: application/json" https://localhost:8080/midpoint/ws/rest/users -d #user.json -v
And my python script is:
import requests
import json
headers = {
'Content-Type': 'application/json',
}
with open('user.json') as j:
data = json.load(j)
response = requests.post('https://localhost:8080/midpoint/ws/rest/users', headers=headers, data=str(data), auth=('Administrator', 'password'))
print(response)
Can anyone see a reason why my python script would be failing? I am at a loss.
str(data) returns the Python representation of data, not its JSON representation. These two forms can differ in things like ' vs. ", True vs. true, and None vs. null. To properly JSONify data, call json.dumps() on it:
response = requests.post(..., data=json.dumps(data))
or let requests do the JSONification:
response = requests.post(..., json=data)
or use the JSON as it appears in user.json directly:
with open('user.json') as j:
data = j.read()
response = requests.post(..., data=data)