digitalcredentials / security-document-loader

A common documentLoader for DCC JSON-LD using libraries.
MIT License
0 stars 1 forks source link

Responses with JSON Content-Type cause "response body already used"-type errors #13

Closed beaulac closed 5 months ago

beaulac commented 5 months ago

The change to use @digitalbazaar/http-client (https://github.com/digitalcredentials/security-document-loader/pull/11) introduced an incompatibility with responses that have application/json or application/ld+json content-types.

By default, the http-client parses the body if the response content-type includes "json", so when response.text() is called, this throws an error since the response body was already consumed (messages like "body stream already read" (chrome), "Body already used" (react-native), "Body is unusable" (node))

Simple repro:

import {securityLoader} from '@digitalcredentials/security-document-loader';

const ldDocumentLoader = securityLoader({fetchRemoteContexts: true});
// Clear statically loaded contexts to force remote fetch
(ldDocumentLoader as any).documents.clear();
const documentLoader = ldDocumentLoader.build();

documentLoader('https://www.w3.org/2018/credentials/v1').catch(console.error);
dmitrizagidulin commented 5 months ago

PR #14 published as npm 5.0.1