Skip to content

Commit af0919f

Browse files
module: add application/json in accept header when fetching json module
1 parent 20b996d commit af0919f

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

lib/internal/modules/esm/fetch_module.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,9 @@ function fetchWithRedirects(parsed) {
145145
}
146146
const handler = parsed.protocol === 'http:' ? HTTPGet : HTTPSGet;
147147
const result = (async () => {
148+
const extension = parsed.pathname.split('.').pop();
148149
const req = handler(parsed, {
149-
headers: { Accept: '*/*' },
150+
headers: { Accept: extension === 'json' ? 'application/json,*/*;' : '*/*;' },
150151
});
151152
// Note that `once` is used here to handle `error` and that it hits the
152153
// `finally` on network error/timeout.

test/es-module/test-http-imports.mjs

+7
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,11 @@ for (const { protocol, createServer } of [
6767
// ?body sets the body, string
6868
const server = createServer(function(_req, res) {
6969
const url = new URL(_req.url, host);
70+
71+
if (url.pathname.includes('json')) {
72+
assert.strictEqual(_req.headers.accept, 'application/json,*/*;');
73+
}
74+
7075
const redirect = url.searchParams.get('redirect');
7176
if (url.pathname === '/not-found') {
7277
res.writeHead(404);
@@ -203,6 +208,8 @@ for (const { protocol, createServer } of [
203208
import(notFound.href),
204209
{ code: 'ERR_MODULE_NOT_FOUND' },
205210
);
211+
// Import file with json extension
212+
await import(url.href + 'empty.json');
206213

207214
server.close();
208215
}

0 commit comments

Comments
 (0)