0

When I make a GET request that passes through grunt-connect-proxy that returns a 204, no content-type is set. If I make the same request but instead using PUT, grunt-connect-proxy (or grunt-contrib-connect - i don't know) is attaching Content-type: application/json to the response automatically. This is a problem because 1) There is no content so it's inaccurate to say the content-type is anything. and more importantly 2) Angular tries to parse the empty response as JSON which breaks my application.

I've tested the endpoint through my browser and the actual server is not sending this additional information back.

How do I prevent grunt-connect-proxy (or grunt-contrib-connect) from adding a content type to a 204 No Content response?

micah
  • 7,596
  • 10
  • 49
  • 90

1 Answers1

0

I've fixed the problem but I'm not sure if it's a hack. I don't think other people would have to do this because no one else has complained on the internet about it. My solution was to add a middleware to watch the 'header' event on the response and if the statusCode was 204 and the content-type was set to delete the content-type from the headers.

var apply204 = function(req, res, next) {
    res.on('header', function() {
        if(res.statusCode === 204) {
            if(res._headers['content-type'] !== undefined) {
                delete res._headers['content-type'];
            }
        }
    });

    next();
};

Still would be interested in why this happens.

micah
  • 7,596
  • 10
  • 49
  • 90