pythonpython-requests

Empty responnse in the Terminal


Im trying to make a Jokes Generator with an API.This is what i do:

import requests
print("Welcome to Jokes Generator")
devjokes_data = requests.get("https://v2.jokeapi.dev/joke/Any?blacklistFlags=nsfw,religious,sexist&type=twopart").text
setup = devjokes_data[3].replace('"','')
punchline = devjokes_data[4].replace('"','')
print(setup)
print(punchline)

But when i do that.I got this response:

Welcome to Jokes Generator
 

I didn't know what was happening. I expected it to print out the setup and the punchline. But it's just empty. I didn't know how to solve the problem.


Solution

  • Below Code should work....

    import requests
    print("Welcome to Jokes Generator")
    devjokes_data = requests.get("https://v2.jokeapi.dev/joke/Any?blacklistFlags=nsfw,religious,sexist&type=twopart").json()
    setup = devjokes_data.get("setup")
    punchline = devjokes_data.get("delivery")
    print(setup)
    print(punchline)
    

    The issue can be that you are using

    requests.get("apiEndPoint").text

    that gets all the response in text and handling string as array will not work properly thats why your output was empty.. this code will change the response to JSON or dict and easily fetch your data