Search code examples
python-3.xhttppython-requestshttpresponse

how to get response data from a python post request


I have an openapi3 server running that I'm trying to authenticate with.

I can do it with curl with no problems

curl -k -X POST "https://localhost:8787/api/login/user" -H  "accept: application/json" -H  "Content-Type: application/json" -d "{\"username\":\"admin\",\"password\":\"admin\"}"

#and i get the token back
eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VybmFtZSI6ImFkbWluIiwicm9sZSI6ImFkbWluIiwiaWF0IjoxNjEyODY3NDU3LCJleHAiOjE2MTI4NzEwNTd9.9eFgomcpJbinN7L4X1VOHfZGvJeUvHiv6WPjslba1To

but when using python I only get the response code (200) with no data including the token

here is my python script

import requests
import os

url = str(os.environ.get('API_SERVER_URL'))+'login/user'
head = {'accept': 'application/json', 'Content-Type': 'application/json'}
data = {"username": "admin", "password": "admin"}             }
response = requests.post(url, json=data, headers=head, verify=False)

print(response)

All I get with this is a 200 response

<Response [200]>

Solution

  • How about getting the content of the response?

    For example:

    print(response.text)
    

    Or if you expect a JSON:

    print(response.json())