apity icon indicating copy to clipboard operation
apity copied to clipboard

Specifying directives like `charset` in the response `Content-Type` breaks JSON response parsing in v0.0.5

Open jchen42703 opened this issue 9 months ago • 0 comments

Description

If you have a response that sets directives in the Content-Type header, this breaks the response parsing in version v0.0.5. For example, if you specify charset ("application/json; charset=UTF-8"), then this leads to the response.json() call being skipped:

const JSON_CONTENT_TYPES = ['application/json', 'application/ld+json']
....
const contentType = response.headers.get('content-type');
    if (contentType && JSON_CONTENT_TYPES.includes(contentType)) {
        return await response.json();
    }
    else if (contentType && contentType.indexOf('text') === -1) {
        return await response.blob();
    }
    const text = await response.text();

This results in response bodies that are Blob instead of an object.

Solution

Instead of JSON_CONTENT_TYPES.includes, it should check if contentType contains any of the elements in JSON_CONTENT_TYPES

Similar, but not exact logic is in the regular fetcher.ts.

The temporary solution is to just not return Content-Type directives (but that's not necessarily the ideal solution).

jchen42703 avatar May 14 '24 03:05 jchen42703