krakenjs / fetch-robot

Proxy fetch through an iframe

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Response without body cause an error during deserialization

beckelmw opened this issue · comments

We have an API that responds with 201s and 204s which have a null response body. This results in the following error:

TypeError: Failed to construct 'Response': Response with null body status cannot have body
at fetch-robot.js:1363

This is the code:

             function deserializeResponse(response) {
                return response.text().then(function(text) {
                    return new window.Response(text, {
                        status: response.status,
                        statusText: response.statusText,
                        headers: deserializeHeaders(response.headers)
                    });
                });
            }

Should be a simple fix to construct the object and only add the statusText if it is not null.

I tried to fork the project so that I could submit a PR to fix this but could not get it to build or run tests with the dependencies that were installed unfortunately.

This change in serdes.js seems to fix the issue:

export function deserializeResponse(response : SerializedResponseType) : ZalgoPromise<Response> {
    return response.text().then(text => {
        text = text || null; // <--------------------------------------- Added this line
        return new window.Response(text, {
            status:     response.status,
            statusText: response.statusText,
            headers:    deserializeHeaders(response.headers)
        });
    });
}