Skip to content

Body of POST request is empty #2017

Closed
Closed
@kareemmahlees

Description

@kareemmahlees

Note: labeling this as a bug is somewhat misleading but this is the closest I could get to.
Note 2: I know there is some silly mistake I made here but I can't find it.

Description
I have a Nextjs application with a trpc backend, am using openapi-ts to generate the types from an external API service and then instantiating two clients:

  1. openapi-react-query client for client-side operations.
  2. openapi-fetch client for server-side calls that happen inside trpc procedures
    The issue is when I try to issue a POST request to a certain endpoint with a JSON body, the API receives a blank body!, and trying with plain fetch works with no issues.

Here is the code that doesn't work ( I even tried to set the fetch client and the Content-type header manually ):

await ctx.whatsapp.POST("/message/send/{user_id}", {
  fetch: fetch,
  params: {
    path: {
      user_id: ctx.session.user.id,
    },
    body: {
      message: {
        message: "MESSAGE_BODY_HERE",
        phone: "PHONE_NUMBER_HERE",
      },
    },
  },
  headers: {
    "Content-type": "application/json",
  },
});

And here is the code that works when using fetch:

await fetch(
  `${BASE_SEND_URL}/message/send/${ctx.session.user.id}`,
  {
    method: "POST",
    body: JSON.stringify({
      message: "MESSAGE_BODY_HERE",
      phone: "PHONE_NUMBER_HERE",
    }),
    headers: {
      "Content-type": "application/json",
    },
  },
);

And here is how I am instantiating the client in trpc context:

export const createTRPCContext = async (opts: { headers: Headers }) => {
	// snip
  const client = createClient<paths>({
    baseUrl: BASE_URL_HERE,
  });

  return {
   	// snip
    whatsapp: client,
  };
};

Reproduction

  • Create a NextJs app with trpc setup.
  • Instantiate an openapi-fetch client and pass it to trpc context.
  • try to use the client with a POST request.

Expected result

(in case it’s not obvious)

Checklist

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