2

I'm trying to convert the cURL to Python request but doesn't work.

cURL: curl -kv -H 'Content-Type: application/json' 'https://IP-address/api/v1/login' -d '{"username":"api", "password":"APIPassword"}'

My Python requests code:

import requests

url = "https://IP-address/api/v1/login"

payload = "'{\"username\":\"api\", \"password\":\"APIPassword\"}'"
headers = {
    'Content-Type': "application/json",
    'cache-control': "no-cache",
    }

response = requests.request("GET", url, headers=headers, data=payload, verify=False)

print(response.text)

Which doesn't work and gives me 400 bad requests error.

I tried converting using the https://curl.trillworks.com/

which gives me the following code which doesn't work either.

import requests

url = 'https://IP-address/api/v1/login'

headers = {
    'Content-Type': 'application/json',
}

data = '{"username":"api", "password":"APIPassword"}'

output = requests.get(url, data=data, verify=False)

print (output)

Can anyone please help me identify the issue here.

Edit: I have edited 2nd script to produce output: Which gives 500 Error

9
  • What about the flags that you've specified in the cURL command? Why are you not including them in your python script? Commented Jul 20, 2017 at 6:10
  • The 400 bad request probably stems from your original payload being invalid JSON. Your curl command has single quotes (') around the JSON, but those are just so bash would interpret the whole thing as a single argument.
    – Kendas
    Commented Jul 20, 2017 at 6:11
  • 1
    @AakashVerma -k is --insecure, it is the same as verify=False; -v is --verbose; -H is --header; -d is --data
    – Kendas
    Commented Jul 20, 2017 at 6:15
  • Your own python code does have mistakes. In your second code, what does "doesn't work either" mean? 400 or 403 or something else?
    – mononoke
    Commented Jul 20, 2017 at 6:16
  • @mononoke You're right - the second code snippet doesn't actually print out the response
    – Kendas
    Commented Jul 20, 2017 at 6:18

2 Answers 2

2

Use the json parameter in requests.post for json data. It also takes care of the headers.

data = {"username":"api", "password":"APIPassword"}
response = requests.post(url, json=data, verify=False)
2
  • This appears to be working, I'm getting response as 200. But how do I see the actual output? When I run the curl, I get a session-token as output, but how do I get in Python requests?
    – Karthik
    Commented Jul 20, 2017 at 6:52
  • Just print it, ie: print(response.content) or if it's a json response use: print(response.json())
    – t.m.adam
    Commented Jul 20, 2017 at 6:55
0

Another way to make sure you're sending valid JSON in your payload would be to use the json python library to format your payload via json.dumps(), which returns a string representing a json object from an object. This was especially useful to me when I needed to send a nested json object in my payload.

import json 
import requests

url = 'https://sample-url.com' 
headers = { 'Content-Type': 'application/json', 'Authorization': f'{auth_key}'}
payload =   {   "key": "value",
                "key": ["v1", "v2"],
                "key": {
                         "k": "v"
                        }
                ...
            }

r = requests.post(url, headers=headers, data=json.dumps(payload))

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.