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)
Related
There is some api endpoint, which i try to send POST request with code like this
import requests
url = 'https://deviantart.com/api/v1/oauth2/collections/folders/create'
headers = {'Content-Type': 'application/json'}
data = {'folder': 'folder_name'}
params = {'access_token': '<authorization_code_flow_token>'}
r = requests.post(url=url,
headers=headers,
params=params,
json=data)
print(r.text)
But i get 400 response:
{
"error":"invalid_request",
"error_description":"Request field validation failed.",
"error_details":{"folder":"folder is required"},
"status":"error"
}
I don't understand why it fails, because their example with curl works fine.
curl https://www.deviantart.com/api/v1/oauth2/collections/folders/create \
-d "folder=Awesome Collection" \
-d access_token=Alph4num3r1ct0k3nv4lu3
And I was successful with post responses to get authenticated.
I tried to change content-type header(json and x-www-formurlencoded) and pass data-payload different ways(passing json string to data param, passing dict to json, passing paylod as query string). But It does not work. I dont have a clue what i am doing wrong. It seems like i send payload wrong or put wrong headers, but i tried a lot of "combinations" and still no effect.
For next hour you if you want try to help you can use access_token:
ba4550889c8c36c8d82093906145d9fd66775c959030d3d772
The following code will work.
import requests
url = "https://www.deviantart.com/api/v1/oauth2/collections/folders/create"
payload={'folder': 'Awesome Collection',
'access_token': 'ba4550889c8c36c8d82093906145d9fd66775c959030d3d772'}
files=[]
headers = {}
response = requests.request("POST", url, headers=headers, data=payload, files=files)
print(response.text)
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))
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.
I wish to convert my current curl command to python code to request for access token for an api.
curl -X POST -H "Content-Type:application/json" -d '{"grantType":"client_credentials"}' [Access Token URL]
my attempt:
import requests
api_url_base = "https://api.example.com/api/"
headers ={'Content_Type':'application/json',
'grandType': 'client_credentials'
}
response = requests.post(headers=headers, "https://api.example.com/accesstokens")
if response.status_code == 200:
print(json.loads(response.content.decode('utf-8')))
else:
print(response.status_code)
Expected output:
Should be in python.
You're mismatching posting headers and body data there, not to mention there's a typo with grandType.
Either way, posting JSON and parsing JSON responses is super easy with Requests:
response = requests.post(
"https://api.example.com/accesstokens",
json={"grantType": "client_credentials"},
)
response.raise_for_status() # raise an exception for error statuses
data = response.json()
print(data)
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.