HTTP/2 pseudo-header used in an invalid context
Production Risk
Causes stream or session errors; ensure header construction is direction-aware.
Thrown when an HTTP/2 pseudo-header is used in an incorrect direction or position. Request pseudo-headers (:method, :path, :scheme, :authority) cannot appear in responses; the response pseudo-header (:status) cannot appear in requests. All pseudo-headers must appear before regular headers.
- 1Including :status in a client request
- 2Including :method or :path in a server response
- 3Placing pseudo-headers after regular headers
Triggered when the HTTP/2 stack validates headers and finds a pseudo-header in the wrong direction.
const req = client.request({
':method': 'GET',
':path': '/',
':status': '200', // invalid in a request
});expected output
Error [ERR_HTTP2_PSEUDOHEADER_NOT_ALLOWED]: Cannot set HTTP/2 pseudo-headers
Fix
Use pseudo-headers only in their correct direction
WHEN When constructing HTTP/2 headers
// Client request
const reqHeaders = { ':method': 'GET', ':path': '/', ':scheme': 'https', ':authority': 'example.com' };
// Server response
stream.respond({ ':status': 200, 'content-type': 'text/plain' });Why this works
Keeping pseudo-headers direction-correct and before regular headers satisfies HTTP/2 validation.
const req = client.request({
':method': 'GET',
':path': '/',
':status': '200', // invalid in a request
}); // this triggers ERR_HTTP2_PSEUDOHEADER_NOT_ALLOWEDtry {
// operation that may throw ERR_HTTP2_PSEUDOHEADER_NOT_ALLOWED
riskyOperation()
} catch (err) {
if (err.code === 'ERR_HTTP2_PSEUDOHEADER_NOT_ALLOWED') {
console.error('ERR_HTTP2_PSEUDOHEADER_NOT_ALLOWED:', err.message)
} else {
throw err
}
}// Validate inputs before calling the operation
function safe_err_http2_pseudoheader_not_allowed(...args) {
// validate args here
return performOperation(...args)
}✕ Mix request and response pseudo-headers
Direction-specific pseudo-headers cause immediate validation errors if used in the wrong context.
Node.js Error Codes Documentation
Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev