0

I need to get a key value of HttpResponse from some URL and send the value again to the url to get some results through Restapi

resp = requests.get('https://someurl.io')
data = HttpResponse(resp)
return data

with this code i got a response like,

{"status":200,"result":{"postcode":"BN14 9GB","quality":1,"eastings":515087,"northings":105207,"country":"England","nhs_ha":"South East Coast","longitude":-0.36701,"latitude":50.834955,"european_electoral_region":"South East","primary_care_trust":"West Sussex","admin_ward":"Offington","ced":"Cissbury","ccg":"NHS Coastal West Sussex","nuts":"West Sussex (South West)","codes":{"admin_district":"E07000229","admin_county":"E10000032","admin_ward":"E05007703","parish":"E43000150","parliamentary_constituency":"E14000682","ccg":"E38000213","ced":"E58001617","nuts":"UKJ27"}}}

I have to send only the latitude and longitude value to some URL and get results. I would like to do this in rest api. I dont know how to get only latitude and longitude value and through the value how to get response from someurl.io.

1
  • You can obtain the dictionary of the result with data.json()['result'], and thus then construct a new dictionary. Commented May 25, 2019 at 12:27

1 Answer 1

1

You can obtain a Python dictionary with data.json(), and then do some processing to construct a new dictionary, like:

resp = requests.get('https://someurl.io')
resp_data = resp.json()['result']
result_dic = {
    'longitude': resp_data['longitude'],
    'latitude': resp_data['latitude']
}

# ...

You can for example return the dictionary as:

return JsonResponse(result_dic)

and this will thus contain a dictionary with two keys: 'longitude' and 'latitude'. You can of course pass other values as well.

Sign up to request clarification or add additional context in comments.

6 Comments

but i can get values only this command return JsonResponse(result_dic['longitude'], safe=False) it doesnot work if i give return JsonResponse(result_dic['longitude'], result_dic['longitude'], safe=False) why??
@Divyaprabha: please don't return a non-dictionary as JsonResponse, there have been several hacks with this: haacked.com/archive/2008/11/20/… that is why safe=... is used here.
@Divyaprabha: no just JsonResponse(result_dic), don't make it harder than it is.
TypeError Exception Value: string indices must be integers got this error.. That's why I wrote like that.
@Divyaprabha: did you by any chance still use data = HttpResponse(resp)?
|

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.