0

Write a function named "query_string" that doesn't take any parameters. The function will make an HTTPS GET request to the url "https://fury.cse.buffalo.edu/ps-api/a" with a query string containing the key-value pairs x=5, y=4, and z=5. The response from the server will be a JSON string representing an object in the format "{"answer": }" where is a floating point Number. Return the value at the key "answer" as a float

    import urllib.request
    import json

    def query_string():
        response = urllib.request.urlopen("https://fury.cse.buffalo.edu/ps-api/a")
        content_string = response.read().decode()
        content=json.loads(content_string)
        return float(content['answer'])

output: function query_string incorrect on input []

returned: -1.0 expected: 119.99

any idea how i can fix this issue?

4
  • You forgot about the "query string containing the key-value pairs x=5, y=4, and z=5" part. Commented Nov 27, 2018 at 18:06
  • I'm new to python, well coding in general, do i put that on the end after the /a" in the url? Commented Nov 27, 2018 at 18:07
  • Yes your url should be like "fury.cse.buffalo.edu/ps-api/a?x=5&y=4&z=5" Commented Nov 27, 2018 at 18:12
  • I tried that , the url you provided response = urllib.request.urlopen("fury.cse.buffalo.edu/ps-api/a?x=5&y=4&z=5") the output said : error on input []: unknown url type: 'fury.cse.buffalo.edu/ps-api/a?x=5&y=4&z=5' Commented Nov 27, 2018 at 18:16

2 Answers 2

1

You can do something like this using the requests package. It's super helpful. You can add the query parameters in a dict that gets passed to the params keyword argument

def query_string():
    import requests
    url=r'https://fury.cse.buffalo.edu/ps-api/a'
    payload={
    'x':5,
    'y':4,
    'z':5}
    r=requests.get(url,params=payload)
    j=r.json()
    print(j)

EDIT for urllib

def query_string():
    url=r'https://fury.cse.buffalo.edu/ps-api/a'
    payload={
    'x':5,
    'y':4,
    'z':5}
    url=url+'?'+urllib.parse.urlencode(payload)
    r=urllib.request.urlopen(url).read().decode()
    r=json.loads(r)
    return float(r['answer'])
Sign up to request clarification or add additional context in comments.

3 Comments

Yeah I wanted to use requests as the library, but my professor has blocked that, he wants us explicitly to use urllib
@codernoob3232 Well thats super lame, but i made an edit for using only urllib
i figured it out on my own but i tested yours as well and it worked, ill upvote it as the answer as well, appreciate your help man.
0
import urllib.request
import json

def query_string():
    url = "https://fury.cse.buffalo.edu/ps-api/a"
    url = url + "?x=5&y=4&z=5"
    response = urllib.request.urlopen(url)
    content_string = response.read().decode()
    content=json.loads(content_string)
    return float(content['answer'])

ended up fixing it after a little research

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.