Replies: 12 comments 1 reply
-
Would you please provide a reproduction? 🙏🏼 (please make sure you are actually making a POST request) |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
I just used HTTP client from vscode and Post method works just as expected but when using ofetch or axios from my frontend project, I get the same error |
Beta Was this translation helpful? Give feedback.
-
I am making the post request from a different project also running local server @PORT:8080 |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
-
I believe this is an internal error, I was able to solve this with a one on one help from @danielroe by creating a file in the middleware folder like this export default defineEventHandler((event) => {
if (event.node.req.method === 'OPTIONS')
return null
}) |
Beta Was this translation helpful? Give feedback.
-
To clarify, this should be in the same handler (which should not be only a POST handler) rather than in the middleware directory. The issue was that we had to handle CORS requests in this particular setup |
Beta Was this translation helpful? Give feedback.
-
@danielroe |
Beta Was this translation helpful? Give feedback.
-
export default defineEventHandler(async (event) => {
if (event.method === 'OPTIONS') {
// set your options here
handleCors(event, {})
return null
}
assertMethod(event, ['POST'])
const body = await readBody(event)
console.log('Body:', body)
// TODO: Handle body and add user
return { body }
}) |
Beta Was this translation helpful? Give feedback.
-
@danielroe, |
Beta Was this translation helpful? Give feedback.
-
I'm not sure I understand the issue. The OPTIONS request should not perform additional work other than returning the correct headers. |
Beta Was this translation helpful? Give feedback.
-
@danielroe, this only works if I remove the .post suffix from the file and comment out the handleCors() composable. |
Beta Was this translation helpful? Give feedback.
-
Environment
Linux OS
Ubuntu 22.04
Reproduction
reproduction
Describe the bug
I set up a nitro server mainly as an API/backend for a mobile app
everything seems to work fine but following the examples on the documentation , HTTP POST requests return no response from the server:
Adding .post.ts to my route/filename
This does nothing!
And when I remove the .post suffix, h3 composable utils like readBody() throws error
H3Error: HTTP method is not allowed
What am I doing wrong?
Additional context
No response
Logs
No response
Beta Was this translation helpful? Give feedback.
All reactions