Search code examples
pythonjsonrequestheader

How to get json text by request with header details?


I would like to go to page https://losoviny.iamroot.eu/part_one and write json text from there. End at first I must login in https://losoviny.iamroot.eu/part_one_login and use details in header.

But if If I run code I see:

raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

Do you know how to rewrite it? (Header data are correct)

Thank you

import json
import requests

headers = {
    'username': 'Loskarlos',
    'password':  'JednohoDneOvladnuKSI'
}


response = requests.post('https://losoviny.iamroot.eu/part_one_login', headers=headers)
        
response_get = requests.get('https://losoviny.iamroot.eu/part_one ')
    
response_get = json.loads(response_get.json())
    
print(response_get) 

Solution

  • First you need to get the token from https://losoviny.iamroot.eu/part_one_login in order to test the API use postman. Your initial response is not a header element. It is a form, use below code to get the token.

    import requests
    
    url = "https://losoviny.iamroot.eu/part_one_login"
    
    payload={'username': '<User NAME>',
    'password': '<Password>'}
    
    headers = {}
    
    response = requests.request("POST", url, headers=headers, data=payload)
    
    print(response.text)
    

    After this I have no idea to pass the token header as per below code. Use header parameters when consuming the part_one endpoint. For header use Bearer format to build the Authorization header parameter.

    import requests
    
    url = "https://losoviny.iamroot.eu/part_one"
    
    payload={}
    headers = {
      'Authorization': 'Bearer <TOKEN>'
    }
    
    response = requests.request("GET", url, headers=headers, data=payload)
    
    print(response.text)
    

    As a practice use postman like tool to navigate an API. cheers!!!