2

I'm calling a WebAPI controller from jQuery AJAX to request an item be deleted via a REST API.

The WebAPI controller returns a 204 (No Content) response, which then invokes a second request (GET). For this example I expect both requests to receive a 204 (No Content) response.

When viewed in Chrome both responses are fully recognised.

enter image description here

However, Firefox can't seem to cope with a 204 if it contains no content.

To obtain both the Chrome (above) and Firefox (below) screenshots the same requests were sent to a WebAPI controller from both browsers. The screenshot below shows that Firefox only recognises the first 204 response.

enter image description here

The response to these two requests is created differently:

The first response uses...

return Request.CreateResponse(HttpStatusCode.NoContent);

...which returns an instance of HttpResponseMessage.

The second response uses...

return new NoContentResult();

...which we've defined as an implementation of IHttpActionResult...

public class NoContentResult : IHttpActionResult
{
    private readonly HttpRequestMessage _request;

    public NoContentResult(HttpRequestMessage request)
    {
        _request = request;
    }

    public Task<HttpResponseMessage> ExecuteAsync(CancellationToken cancellationToken)
    {
        var response = _request.CreateResponse(HttpStatusCode.NoContent, new StringContent("{}"));
        return Task.FromResult(response);
    }
}

The new StringContent("{}") on the 4th-from-last line is an attempt to provide some content to help Firefox. I've also tried new StringContent("null") with the same result.

Also, if that second response is inspected in Firefox, the following error message is found:

SyntaxError: JSON.parse: unexpected end of data at line 1 column 1 of the JSON data

It seems that Firefox is trying to parse this problematic 204, but it apparently makes no attempt to parse the first 204 response.

Furthermore, the request type (DELETE vs. GET) doesn't seem to matter: if I return 204 as an IHttpActionResult from the first call then Firefox still doesn't like it.

Firefox also displays the first response as containing 168 bytes and the second as containing 0 bytes even though it will happily display the headers (below). Firefox is clearly upset at something.

enter image description here

Can anyone explain why Firefox has a problem with a 204 response when delivered as an implementation of IHttpActionResult but not when it's delivered as an HttpResponseMessage?

[The reason that the first request returns an HttpResponseMessage while the second returns an IHttpActionResult is that the first method has been in place for a long time whereas the second method is only now being added. And as IHttpActionResult is easier to test, we'd like to use that.]

awj
  • 7,482
  • 10
  • 66
  • 120
  • Out of curiosity, if you return `StatusCode(HttpStatusCode.NoContent)`, does that satisfy Firefox? – DylanSp Mar 18 '19 at 20:35

1 Answers1

0

I had a similar issue with Firefox choking at status code 204 because the response contained Content-Type header which is not in accordance with the specification. Removing the Content-Type header from the 204 response, it started receiving it fine. Giving this, I decided to go a little further and remove all irrelevant header fields from the 204 response.

On the other hand, Chrome/Webkit based browsers don't seem to have any issues with that useless Content-Type header when processing a 204 response. 204 response successfully by Firefox

Alexandre M
  • 1,146
  • 12
  • 23