Skip to content

Fails to parse responses when Content-Length header is stripped by proxy #2574

@brunolca

Description

@brunolca

openapi-fetch version

0.13.8

Description

When a response with an empty body passes through Cloudflare (or other proxies), the client attempts to parse the empty response as JSON and fails. This happens because the current logic incorrectly treats the absence of a Content-Length header as indicating content is present.

Reproduction

// Mock server that returns empty body without Content-Length header
import express from 'express';

const app = express();

app.post('/test', (req, res) => {
  res.removeHeader('Content-Length');
  res.status(200).end();
});

app.listen(3000);

Or simulate with fetch mock:

global.fetch = async () => new Response(null, {
  status: 200,
  headers: {} // No Content-Length header
});

// Now use openapi-fetch - it will fail trying to parse empty body as JSON

Expected result

The client should gracefully handle empty responses regardless of whether the Content-Length header is present or absent. When a response has no body, response.data should be undefined or an appropriate empty value without attempting to parse it as JSON.

Extra

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingopenapi-fetchRelevant to the openapi-fetch library

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions