I want to make GET, POST & PUT calls to a 3rd party API and display the response on the client side via AJAX. The API calls require a token, but I need to keep that toke
Because all you want is to add token to http headers
, which i am assuming is Authorization
a simple way would be to implement a proxy server that makes calls to your api endpoint after adding up those. A sample file for nginx
would be
location /apiProxy {
proxy_pass http://www.apiendPoint.com/;
proxy_set_header Authorization ;
}
This is a much more smarter approach rather than writing a program and gets you off with 4 lines of code. Make sure to change your parameters accordingly and add other parameters as needed by api client you are using. The only difference on javascript side would be to use the location url
rather than one provided by service which acts as a proxy.
The configuration for apache
would be
NameVirtualHost *
ProxyPass http://www.apiendPoint.com/
ProxyPassReverse http://www.apiendPoint.com/
Header add Authorization ""
RequestHeader set Authorization ""