Search code examples
pythonjsonpython-requests

Python Requests - response.json() not working as intended


I'm trying to call an API from Python's requests module. On postman, the Content-Type in the response headers returned is application/json; charset=utf-8, and the response json data is how I'd expect it to look like. However, running response.json() after the get method for the API on python throws the error simplejson.errors.JSONDecodeError: Expecting value: line 1 column 1 (char 0).

Hope someone could help explain why this happens.

First few lines of the content returned on Postman:

{
    "Pagination": {
        "query": "",
        "index": "PRODUCTION_PG_FEATURED_DESC",
        "limit": "100",
        "page": 1,
        "total": 8556,
        "lastPage": "/api/v3/browse?_tags=adidas&page=10&productCategory=sneakers&resultsPerPage=100",
        "sort": [
            "featured"
        ],
        "order": [
            "DESC"
        ],
        "currentPage": "/api/v3/browse?_tags=adidas&page=1&productCategory=sneakers&resultsPerPage=100",
        "nextPage": "/api/v3/browse?_tags=adidas&page=2&productCategory=sneakers&resultsPerPage=100",
        "previousPage": null
    }, 

My code block:

import requests

productsAPI = 'https://stockx.com/api/browse?_tags=nike&productCategory=sneakers&page=1&resultsPerPage=100'
headers = {"accept": "*/*", "accept-language": "en-US,en;q=0.9", "accept-encoding" : "gzip, deflate, br","User-Agent" :"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.76 Safari/537.36", 
        "Connection" : "keep-alive"}
response = requests.get(productsAPI, headers = headers)
print(response.text)
        
if response.status_code != 200:
    print(response.status_code)
try: 
    data = response.json()
except:
    print('fail')

print(data["Facets"]["brand"])


Solution

  • Looking at your code, I'm not sure how you don't see the obvious problem. You have this header:

        "accept-encoding" : "gzip, deflate, br",
    

    and the server happily took your suggestion and deflated and gzipped the response. If you aren't prepared to accept encodings, then get rid of the line that says you will. That fixes it.