Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Why does using beforeFetch cause a POST request to become a GET request? #4677

Unanswered
PeiTianHuang asked this question in Q&A
Discussion options

The value of fetchOptions is as follows:
{ "method": "POST", "body": "{\"dates\":[\"2025-03-17T02:41:31.375Z\",\"2025-03-24T02:41:31.375Z\"],\"pageName\":\"\",\"pageSize\":10,\"currentPage\":1,\"total\":100,\"data\":[]}", "headers": { "Content-Type": "application/json" } }

If I don't set beforeFetch, the request I'm initiating at this moment is a POST request
return useFetch( url.startsWith('http') ? url : ${baseURL}${url}, { ...fetchOptions, } )

But if I set beforeFetch, a GET request will be initiated regardless of whether the returned option is the same as fetchOptions
return useFetch( url.startsWith('http') ? url : ${baseURL}${url}, { ...fetchOptions, async beforeFetch({ url }) { return { url, fetchOptions } }, } )

You must be logged in to vote

Replies: 1 comment

Comment options

Hey @PeiTianHuang! 👋 I'm here to help you with any bugs or questions you have. I can also assist you in becoming a contributor.

It seems like you're encountering an issue where using beforeFetch causes a POST request to become a GET request. This might be related to how the beforeFetch function is modifying or not properly handling the fetchOptions.

In the beforeFetch function, ensure that you are correctly returning the options object with the method set to POST. Here's a possible adjustment:

return useFetch(
  url.startsWith('http') ? url : `${baseURL}${url}`,
  {
    ...fetchOptions,
    async beforeFetch({ options }) {
      options.method = 'POST'; // Ensure the method is explicitly set
      return { options };
    },
  }
)

This ensures that the method remains as POST when beforeFetch is used. If the options object is not returned correctly, it might default to GET, as useFetch defaults the method to GET if not explicitly set [1].

To continue talking to Dosu, mention @dosu.


Help Dosu learn! Give it feedback: Great Response | Irrelevant Answer | Incorrect Sources | Too Verbose | Hallucination | Bug Report | Other

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
🙏
Q&A
Labels
None yet
1 participant
Morty Proxy This is a proxified and sanitized view of the page, visit original site.