Python Forum
Requests gets no out put and no error
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Requests gets no out put and no error
#1
using below, I am getting no error, but also no output....anyone able to help?
import requests
import json
import sys


def get_request(api_key):
    api_url='https://api.nasa.gov/planetary/apod'
    r= requests.get(api_url, headers = {"Content-Type": "application/json", "access_key": api_key})
    api_key ='<my api key>'

    print(r.content)
    get_request(api_key)
Reply
#2
(Oct-10-2018, 07:52 PM)water_fox Wrote: using below, I am getting no error, but also no output....anyone able to help?
import requests
import json
import sys


def get_request(api_key):
    api_url='https://api.nasa.gov/planetary/apod'
    r= requests.get(api_url, headers = {"Content-Type": "application/json", "access_key": api_key})
    api_key ='<my api key>'

    print(r.content)
    get_request(api_key)
You may start by checking r.status_code and r.reason
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply
#3
If that is all your code, you just define the get_request() function but never call it. Note that lines#9 and 12 are indented and are in fact part of function itself. Unindent lines#9 and 12 one level. The print should remain part of the function.

import requests
import json
import sys
 
 
def get_request(api_key):
    api_url='https://api.nasa.gov/planetary/apod'
    r= requests.get(api_url, headers = {"Content-Type": "application/json", "access_key": api_key})
    print(r.content)


api_key ='<my api key>'
get_request(api_key)
also if the response is json format, you can use r.json()
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  python requests library .JSON() error mHosseinDS86 6 3,253 Dec-19-2022, 08:28 PM
Last Post: deanhystad
  No route to host error when using requests.post in python on raspberry pi mariummalik22 0 4,028 Jan-06-2018, 08:34 PM
Last Post: mariummalik22
  import requests gives me error blckpstv 1 7,280 Jan-22-2017, 10:41 AM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020