HERE places REST apis returns web ui instead on json

拜拜、爱过 提交于 2019-12-14 03:04:47

问题


I am evaluating HERE places APIs. However, the url does return the web page instead of plain JSON as response. Example query:

https://places.cit.api.here.com/places/v1/autosuggest?app_id={YOUR_APP_ID}&app_code{YOUR_APP_CODE}&at=52.5304417,13.4111201&q=rest&pretty

I need the response as simple JSON but can't find any information on the documentation page.


回答1:


The answer is to add callback parameter to the url. Example:

https://places.cit.api.here.com/places/v1/autosuggest?app_id={YOUR_APP_ID}&app_code{YOUR_APP_CODE}&at=52.5304417,13.4111201&q=rest&pretty&callback=xyz

It is not clear from the documentation. I got this information from HERE support.




回答2:


This is because you were trying the request from your browser address bar, in which case the browser automatically adds Accept: text/html to the request headers⁽¹⁾.

Then, because the Places API has some web helper tool to play with available parameters, it returns html (the Web UI) because Accept: text/html was found in the request headers.

The solution to add callback is more a workaround, since it wraps the response with the value of the callback parameter.

Your initial request was actually valid. If you send it from your program or a REST client like Postman instead of from the browser, you actually get JSON by default, so there was nothing more to do. Of course, you can explicitly send the header Accept: application/json to be on the safe side.

[1] Open the network panel of the browser developer tools to see what is sent.




回答3:


You must set the Accept header in order for the service to respond in JSON.

See the example within the documentation here:

curl \
  -X GET \
  -H 'Accept: application/json' \
  --get 'https://places.demo.api.here.com/places/v1/discover/search' \
    --data-urlencode 'at=37.7942,-122.4070' \
    --data-urlencode 'q=restaurant' \
    --data-urlencode 'app_id={YOUR_APP_ID}' \
    --data-urlencode 'app_code={YOUR_APP_CODE}'


来源:https://stackoverflow.com/questions/50220380/here-places-rest-apis-returns-web-ui-instead-on-json

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!