简体   繁体   中英

How to remove api_key from url in angularjs

I need an apikey to be able to send a get request to a REST api. The request will look something like this -

$http.get( http://api.randomsite.com/page.json?api_key=apikey123 )

This will give me data in json format.

But i dont want the api key to appear in the url due to safety reasons. Is it possible to remove the api_key and somehow still make the get request work? Like this -

$http.get( http://api.randomsite.com/page.json )

I read its something to do with headers, but i tried a lot and cannot figure it out. Help :/

Well, if you want to hide the API_KEY, there's few ways to do this

  1. Send it through POST , although this is only a half measure, as anyone could look into the POST body and see what it is. Nevertheless, the way you do it is by

$http.post("api/salesapi.php/sales/", {API_KEY:xx}, {headers: ... });

  1. Let your server handle it; This hides the API_KEY from the user completely and let the server act as the middle man. But, you need a server to do this, so that's not possible if you're server-less

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM