-1

I was trying to implement an API using Python, and flask to help myself learn and practice REST.

The idea was to receive a HTTP POST with data that looks like as such: {"startDate":"2015-07-01","endDate":2015-07-08","within":{"value":9000,"units":miles}} and send some of the data to a NASA API(endpoint).

I was able to create a POST method , and I am able to receive the data (both in POSTMAN and in the browser). Here is the relevant code :

@neows.route('/UserInput',methods=['GET','POST'])
def UserInput():
    startDate = request.args.get('startDate')
    endDate = request.args.get('endDate')
    #print (type(startDate))
    #print (type(endDate))
    getAsteroids(startDate,endDate)
    return jsonify(request.args)

But when I extract some data from the POST above to send to a NASA API (GET), I am receiving this error:

werkzeug.exceptions.BadRequestKeyError

Here is the url I am trying to hit : (https://api.nasa.gov/neo/rest/v1/feed?start_date=START_DATE&end_date=END_DATE&api_key=API_KEY)

I am able to hit the url both on POSTMAN and browser, outside of my code.

The relevant piece of code with the error is posted below and the line that seems to be throwing the error is in Italics (marked with *).

def getAsteroids(startDate,endDate):
    API_KEY='xxx'
    print (startDate)
    print (endDate)
    *result=request.args["https://api.nasa.gov/neo/rest/v1/feed? 
    start_date="+startDate+"&end_date="+endDate+"&api_key="+API_KEY+""]*

I would really appreciate if some one could help me understand and resolve this issue.

Seanny123
  • 8,776
  • 13
  • 68
  • 124
user1462617
  • 413
  • 1
  • 13
  • 23

1 Answers1

0

If you want to do a request against the NASA's API you can use requests module. (Or any other module to send HTTP requests)

import requests

# ...

def getAsteroids(startDate, endDate):
    API_KEY='xxx'
    payload = {'start_date': startDate, 'end_date': endDate, 'api_key': API_KEY}
    result = requests.get('https://api.nasa.gov/neo/rest/v1/feed', params=payload)

request.args is something different used to get the parameters of the incoming request.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Thom
  • 1,473
  • 2
  • 20
  • 30