I have this error when I am trying to get response after the call of the API I want to use.
ValueError: [TypeError("'property' object is not iterable"),
TypeError('vars() argument must have __dict__ attribute')]
I am trying to use fastapi in order get from the client the latitudes and longitudes so i could show the public trasnportation of that area. I could do this with an API called GeoApify. However, I have a problem and I can not find my error.
I make a request by using a dictionary in order to put all the parameters for my filter and then I convert the response to JSON. But i have this error.
from pickletools import string1
from fastapi import FastAPI
import requests
from requests.structures import CaseInsensitiveDict
app = FastAPI()
@app.get("/thanos/{lon}/{lat}")
async def read_item(lat : float,lon : float):
url = "https://api.geoapify.com/v2/places"
headers = CaseInsensitiveDict()
dict = {
"categories" : 'public_transport',
"filter" : 'circle:' + str(lon) + ',' + str(lat) + ",500",
"limit" : '20',
"apiKey" : '086a77f34e3a4ed583da9606318ca0ac'
}
params = dict
headers = CaseInsensitiveDict(params)
headers["Accept"] = "application/json"
resp = requests.get(url, headers = headers)
# resp = requests.get(url = url, params = params)
data = resp.json
return resp
In your example, you are trying to pass the various parameters as request headers. However, the parameters should be passed to the params argument, as they are expected as query parameters instead.
from fastapi import FastAPI
import requests
from requests.structures import CaseInsensitiveDict
import urllib
app = FastAPI()
url = "https://api.geoapify.com/v2/places"
@app.get("/{lon}/{lat}")
def read_item(lat : float, lon : float):
params = {
"categories" : 'public_transport',
"filter" : 'circle:' + str(lon) + ',' + str(lat) + ",500",
"limit" : '20',
"apiKey" : '086a77f34e3a4ed583da9606318ca0ac'
}
headers = CaseInsensitiveDict()
headers["Accept"] = "application/json"
params = urllib.parse.urlencode(params)
resp = requests.get(url=url, params=params, headers=headers)
return resp.json()
I would also suggest having a look at this answer on how to use an async HTTP client within an async def endpoint instead.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With