Skip to content

API calls do not use the content-type #2344

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
1 task done
aaronbeall opened this issue May 29, 2025 · 1 comment
Open
1 task done

API calls do not use the content-type #2344

aaronbeall opened this issue May 29, 2025 · 1 comment
Labels
bug Something isn't working openapi-react-query Relevant to openapi-react-query

Comments

@aaronbeall
Copy link

openapi-react-query version

0.5.0

Description

POST request made with openapi-fetch and openapi-react-query is missing the content-type header (server expects application/json).

Reproduction

My POST spec specifies the request as application/json

      requestBody:
        description: Prioritization to create
        content:
          application/json:

Which I see generates the expected types:

        requestBody: {
            content: {
                "application/json": components["schemas"]["Prioritization"];
            };
        };

However, when I use the query:

import createFetchClient from "openapi-fetch";
import createClient from "openapi-react-query";

const fetchClient = createFetchClient<paths>({
  baseUrl: "/",
  fetch: (input, init: RequestInit = {}) => {
    const headers: HeadersInit = {
      ...init.headers,
      ...getAuthHeader(),
    };
    return fetch(input, { ...init, headers });
  },
});
const api = createClient(fetchClient);

// ...

const addMutation = api.useMutation("post", "/prioritizations", {
  onSuccess: () => {
    // ...
  },
  onError: () => {
    // ...
  }
});

The network request does not have the correct content-type header:

POST /v1/prioritizations HTTP/1.1
Accept: */*
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.9
Authorization: Bearer  <...>
Connection: keep-alive
Content-Length: 73
Host: wunderfolio:9005
Origin: http://localhost:8089/
Referer: http://localhost:8089/
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136.0.0.0 Safari/537.36

Expected result

POST request should have Content-Type: application/json

Extra

@aaronbeall aaronbeall added bug Something isn't working openapi-react-query Relevant to openapi-react-query labels May 29, 2025
@aaronbeall
Copy link
Author

aaronbeall commented May 29, 2025

Ah, never mind, maybe? It seems that providing init.headers to fetch() was preventing the content-type from being added... I don't know why, that doesn't seem right to me, but using the headers option on ClientOptions worked.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working openapi-react-query Relevant to openapi-react-query
Projects
None yet
Development

No branches or pull requests

1 participant