drewzboto / grunt-connect-proxy

Grunt Connect support for proxying API calls during development
MIT License
423 stars 122 forks source link

Changing headers #10

Closed yfr closed 11 years ago

yfr commented 11 years ago

A possibility to change specific header would be nice. I ran into a problem where i had to remove the referer header to get a CORS request for dev running.

in utils.proxyRequest i added:

req.headers.referer = (!proxy.config.referer) ? '' : req.headers.referer;

and changed config in the gruntfile accrodingly. Would be nice to change headers more generaly. Or i there a better solution?

what do you think?

drewzboto commented 11 years ago

Can you elaborate more on your scenario? I'm haven't run into a scenario where the referer header gets in the way of CORS requests.

This is meant to be a development proxy, so i'm not sure if adding more controls over header is ideal. Will let people vote this up or down as needed.

Here's what I recommend.... you can wrap the proxyRequest function setup as grunt connect middleware with your own function to override the header prior to passing into the proxying:

            livereload: {
                options: {
                    middleware: function (connect) {
                        return [
                            function(req, res, options) {
                                req.headers.referer = '';
                                proxySnippet(req, res, options);
                            },
                            lrSnippet,
                            mountFolder(connect, '.tmp'),
                            mountFolder(connect, 'app')
                        ];
                    }
                }
            }
yfr commented 11 years ago

Thank you! this works nicely.

my Probelm:

I have a grunt server running on localhost:9000 with my app and one rails server on a dev domain (like rails-server.dev) on my local mashine. I had to set the referer in the request to an emtpy string (or delete it) to get the connection to work. Even though i set the changeOrigin to true. This is only if i do a request from the webapp.

If i call the api directly (like localhost:9000/api) the proxy redirects and i get my results back. Without changing the referer.

this i my config:

{
  context: '/api',
  host: 'rails-server.dev',
  rewrite: {
    '^/api': ''
},
drewzboto commented 11 years ago

hmmm, i would hope the rails app would be able to handle referer headers better, but perhaps that is out of your control.

closing the issue as it's unblocked with the alternative middleware. people can vote on this if there is need to do this directly in the proxy.

yfr commented 11 years ago

Yeah. I think this is not happening very often. And your solution is quite easy and clean.

Thanks!