Error in system.net.httpClient.get()

system.net.httpClient

#Set the endpoint URL
url = "https://api.openweathermap.org/data/2.5/weather"

#Declare a variable for system function we are using
myClient = system.net.httpClient()

this works

response = myClient.get(url, {"lat":38.652330, "lon":-121.189773, "appid":"MYAPPID"})

this doesn't

myData = '{"lat":38.652330, "lon":-121.189773, "appid":"MYAPPID"}'
response = myClient.get(url, myData)

How can I use the variable myData in this .get() command? If gives me an error on the lines that say "this doesn't work" and I am not sure why.

Also, if I have headers to add to the request, do I use:
myClient.head(url, myHdrData)

Does the .head command use the same syntax as the .get data?

What error does it give you?

ValueError: Invalid URL - Illegal character in query at index 48: https://api.openweathermap.org/data/2.5/weather?{"lat":38.652330, "lon":-121.189773, "appid":"8a639bbc5012f64a5e334c89b9d02e28"}

1 Like

Because you are making myData a string. The working form submits a dictionary and lets httpClient package it properly.

3 Likes

how do i convert it to a dictionary

myData = '{"lat":38.652330, "lon":-121.189773, "appid":"MYAPPID"}'

Should be

myData = {"lat":38.652330, "lon":-121.189773, "appid":"MYAPPID"}

1 Like

Thanks both of you, does the header work the same syntax?

According to the documentation - Dictionary headers - A dictionary of HTTP headers to send with the request. Defaults to None. [optional]

Answer seems to be yes. Seems like you should do something like headers = {'someKey':'someValue'}. You create a dictionary with curly brackets and pairs of keys and values' myDict = {'key1':'someValue1', 'key2':'anotherValue'} etc.

Highly recommend checking out some python 2.7 course. Here's a cheat sheet version I refer my coworkers to Learn Python 2 (legacy) in Y Minutes

4 Likes