如何获取HttpResponse键值并从某些url获得响应?

时间:2019-05-25 12:25:40

标签: python django httpresponse

我需要从某个URL获取一个HttpResponse的键值,然后将该值再次发送到该URL以通过Restapi获得一些结果

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

使用此代码,我得到的响应是

{"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"}}}

我只需将纬度和经度值发送到某些URL并获得结果。我想在REST API中做到这一点。我不知道如何只获取经度和纬度值,也不知道如何通过someurl.io获得响应。

1 个答案:

答案 0 :(得分:1)

您可以使用data.json()获取Python字典,然后进行一些处理以构建新的字典,例如:

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

# ...

例如,您可以将字典返回为:

return JsonResponse(result_dic)

,因此它将包含带有两个键的字典:'longitude''latitude'。您当然也可以传递其他值。