hoxy icon indicating copy to clipboard operation
hoxy copied to clipboard

304 responses fail with as:'json' interceptor

Open smurphy917 opened this issue 6 years ago • 3 comments

With an as: 'json' interceptor, Proxy attempts to JSON.parse the response body, regardless of response.statusCode. Ideally, this would be skipped and the interceptor callback would be called with some indication of the 304 status code and response.json as undefined. Even better, would be an interceptor filter on response.statusCode for response and response-sent phases. Just a thought!

smurphy917 avatar Jun 26 '18 19:06 smurphy917

Have you tried mimeType? Ideally a server wouldn't use a json mime type in a 304 response, so you could do:

proxy.intercept({
  phase: 'response',
  mimeType: /json/,
  as: 'json'
}, function(req, resp, cycle) {
  // ...
});

In any case, Hoxy doesn't have the ability to filter on status code but that definitely seems like a useful feature, and fits with how filtering is done in general. It wouldn't be hard to add. Then you could do:

proxy.intercept({
  phase: 'response',
  mimeType: /json/,
  status: 200,
  as: 'json'
}, function(req, resp, cycle) {
  // ...
});

I may look into adding it when I get some time. PRs also definitely welcome too :)

greim avatar Jun 27 '18 16:06 greim

I agree that the server shouldn't send a application/json mimeType for a 304, but unfortunately the server I'm working with tends to do just that, and I don't control that server.

I'll try to carve out some time to put a PR together for the statusCode filtering, as well as overall better handling of empty/invalid bodies with as: 'json' interceptors.

smurphy917 avatar Jun 27 '18 16:06 smurphy917

I agree that the server shouldn't send a application/json mimeType for a 304, but unfortunately the server I'm working with tends to do just that, and I don't control that server.

Hahahaha... such is life :P

greim avatar Jun 27 '18 19:06 greim