Rob--W / cors-anywhere

CORS Anywhere is a NodeJS reverse proxy which adds CORS headers to the proxied request.
MIT License
8.69k stars 6.13k forks source link

Adding header in the proxy #227

Open benzata opened 4 years ago

benzata commented 4 years ago

Is it possible to add your header for GET requests inside the CORS-Anywhere PROXY? Instead of putting in the header of the GET request in the app code? Thank you very much in advance!!

Rob--W commented 4 years ago

Yes, if you use CORS Anywhere as a server-side library, then you can use the setHeaders option to add request headers.

Documentation: https://github.com/Rob--W/cors-anywhere#server Example: https://github.com/Rob--W/cors-anywhere/blob/4814647a9d21f14858da282387a4295c17f2851d/test/test.js#L818-L847

benzata commented 4 years ago

Amazing! Thank you very much for the prompt reply!

unbekannt3 commented 4 years ago

Hi, can i also specify Headers that will only be sent if for example a domain rule is met?

Rob--W commented 4 years ago

Do you mean request headers or response headers?

In either case, CORS Anywhere does not have out-of-the-box support for domain-specific headers (request headers / response headers). If you want to change the (request/response) headers based on the requested URL only, then you could use a different http(s) server in front of CORS Anywhere (example) and directly modify request.headers (for request headers) or use response.setHeader (for response headers).

freQniK commented 2 years ago

Yes, if you use CORS Anywhere as a server-side library, then you can use the setHeaders option to add request headers.

Documentation: https://github.com/Rob--W/cors-anywhere#server Example: https://github.com/Rob--W/cors-anywhere/blob/4814647a9d21f14858da282387a4295c17f2851d/test/test.js#L818-L847

I tried to use setHeaders in the server for the "origin" and "referer' headers, but they seem not to change as they are both still https://localhost:port instead of the ones I defined. Is there a way to manipulate these two headers to specify a target of my choice, instead of showing the localhost server domain?

Thank you.

Rob--W commented 2 years ago

I tried to use setHeaders in the server for the "origin" and "referer' headers, but they seem not to change as they are both still https://localhost:port instead of the ones I defined. Is there a way to manipulate these two headers to specify a target of my choice, instead of showing the localhost server domain?

The option should work. How are you setting the option, and how do you know that the headers aren't changing?

freQniK commented 2 years ago

I look at the headers in Firefox Developer->Network for a specific javascript to be downloaded for the page to work. I go to http://ip:8080/http://locaton.com and here is what I see in the Developer:

firefox

I set the option as follows:

 cors_proxy.createServer({
           ...
           ...
           ...
setHeaders:  {"referer": "https://desiredwebsite.com/stuff", "origin" : "https://desiredwebsite.com/stuff"},
           ...
           ...
           ...
}).listen(port, host, function() {
  console.log('Running CORS Anywhere on ' + host + ':' + port);
});

Any help is much apprecaited.

Thanks.