0

I am writing a GET request that takes three query paramters in the end of the URL

Now a URL in my list of urls is

(r'/v1/random/data?{}' .format(urllib.urlencode(args)) , 'GET', getResource),

The method getResource redirects to a class where the parameters are to be extracted from the URL I have printed the local variables using locals() in that particular method and they are as such

{'res': {}, 'args': 'data', 'uri': '/v1/random/data', 'entities': ['', 'v1', 'random', 'data'] }

The curl request for the above output is

curl -H "Accept: application/json" -H "Content-Type: application/json" -X GET http://<ip>:<port>/v1/<random>/data?param1=1&param2=2

I want to be able to extract the parameters param1 and param2 from my curl request. How should I implement this in Python ?

IF you dont understand the error above, I want to create an API in python such that

curl "http://localhost:5000/pulse/?lat=41.225&lon=-73.1"

that is able to accept two values in a GET request

The_Lost_Avatar
  • 954
  • 3
  • 13
  • 28
  • Just to clarify: You want to extract the parameters `param1` and `param2` from you curl request? Where do you get these requests from since you have to pass them to Python? – albert Aug 31 '15 at 13:10
  • @Albert: Yes I want to extract param1 and param2. I have a server running to which we can make these curl requests and I have to return the correct response according to the request. – The_Lost_Avatar Aug 31 '15 at 13:14
  • You need to be a bit more specific. What are you running on the server? Are you using a framework? – Daniel Roseman Aug 31 '15 at 13:38
  • @DanielRoseman: Yes, Gunicorn is the framework and I inherit the class gunicorn.app.base.BaseApplication – The_Lost_Avatar Aug 31 '15 at 13:49

1 Answers1

2

Can you use Requests? If so, it's as simple as (example from home page):

payload = {'key1': 'value1', 'key2': 'value2'}
r = requests.get("http://httpbin.org/get", params=payload)

Update:

To produce the exact query string shown in your last CURL example with Requests, you could do:

p = {'lat':41.225, 'lon'=-73.1 }
r = requests.get( 'http://localhost:5000/pulse/', params=p )
Brian McFarland
  • 7,847
  • 4
  • 32
  • 56
  • The above is a GET API and the more I read the more i get to know that get requests shouldnt have query params in the payload http://stackoverflow.com/questions/978061/http-get-with-request-body – The_Lost_Avatar Aug 31 '15 at 15:34
  • This will put the parameters in the URL string, not a separate HTTP request body/payload as seen in POSTs. `payload` is just the name of the python variable (perhaps poorly chosen). – Brian McFarland Aug 31 '15 at 15:58
  • Indeed poorly choosen then :) +1 for your answer. Can it be done with gunicorn on which my code base is currently built ? – The_Lost_Avatar Aug 31 '15 at 18:41