3

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)

1 Answer 1

7

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))
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks! the request seems better formatted, however, I am still getting an error (400 response) with Python using your code whereas with cURL the response is correct (202 response). Any idea why?
The only other thing that i can see is that you are going to a different url in the cURL as apposed to the requests package. curl = api.datamyne.com/frontend/REST/application requests = http://api.datamyne.com/frontend/REST/applicationsapi.datamyne.com/frontend/REST/applications Maybe try removing s to the end of application
Okay. So i got it to work by using json to format the body. I will update my answer.
you can bypass all the json stuff by using the json param to requests data = {"token": "boKnofR06mzldz5eL00ARwa3B9winzpn", "idApp": 44 } response = requests.post('https://api.datamyne.com/frontend/REST/application', headers=headers, json=data)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.