0

newb to python and requests here. I'm working with an API trying to get a JSON response but am only able to get the text version of it. My line of resp_random_1_100_int.json() returns the error:

*** requests.exceptions.JSONDecodeError: Extra data: line 1 column 4 (char 3) resp_random_1_100_int.json()

but when I run my Insomnia(similar to Postman), I'm able to get a JSON response

here is my code:

resp_random_1_100_int = requests.get(
            f"http://numbersapi.com/random",
   params={
                "min": "0",
                "max": "100"
            }
        )

resp_random_1_100_int.json() # <-- gives error

Here is my response from insomnia:

{
    "text": "1 is the number of Gods in monotheism.",
    "number": 1,
    "found": true,
    "type": "trivia"
}

Here is what it looks like in insomnia: enter image description here

Also, if I only run resp_random_1_100_int.text, the response comes back with a random fact and it works but I'm not able to access the other object attributes with resp_random_1_100_int.number or resp_random_1_100_int.type

i've tried setting the headers with headers = {'Content-type': 'application/json'} but still no luck

Any and all help/direction is appreciated.

Cflux
  • 1,423
  • 3
  • 19
  • 39
  • This [answer](https://stackoverflow.com/a/6386366/10761353) has more on `.json()`. Alternatively, try seeing if running the content through `json.loads()` also throws an error. – Adam Smooch Dec 18 '22 at 01:33
  • Those two requests are not the same at all. The one in the screenshot is `http://numbersapi.com/2`, but the python code is requesting `http://numbersapi.com/random?min=0&max=100` – John Gordon Dec 18 '22 at 01:37
  • @JohnGordon, you are right. that was my bad, I've updated the image. – Cflux Dec 18 '22 at 01:41
  • Also, in the code, you assign `resp_random_1_100_int` as the response variable, but then you try to access `resp_random_year.json()`. Those are two completely different variables. – John Gordon Dec 18 '22 at 01:44
  • @JohnGordon you are right. apparently, i've been at it for one too many hours. I've updated the question to correct the variable names. – Cflux Dec 18 '22 at 01:48

1 Answers1

1

The only difference from the following code and what you posted is that I specified the content type using the headers keyword argument:

import requests
resp_random_1_100_int = requests.get(
        f"http://numbersapi.com/random",
        headers={'Content-type': 'application/json'},
        params={"min": "0", "max": "100"})

 resp_random_1_100_int.json()

Returned:

{'text': '79 is the record for cumulative weeks at #1 on the Billboard charts, held by Elvis Presley.',
 'number': 79,
 'found': True,
 'type': 'trivia'}

Here are the versions of Python and Requests I used:

  • Python 3.9.13
  • Requests 2.28.1
Jurnell
  • 66
  • 2
  • Apparently, I didn't set everything correctly. I have no idea why yours worked and mine didn't when I tried setting the headers. thank you. – Cflux Dec 18 '22 at 01:56