-
Notifications
You must be signed in to change notification settings - Fork 18
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
Unable to access request body when using the router #132
Comments
This is because of how The first parameter passed to the route handling function is of type I will look into this to see if there are any easy ways of bypassing this issue. |
Ok, that is good to know. |
I just hit a similar issue while migrating over to the new JS router. Thanks @radu-matei for the workaround. It appears there's some middleware in itty-router-extras that will allow users to safely parse and embed request bodies. Is that something we can integrate with here? https://www.npmjs.com/package/itty-router-extras#withcontent |
The last time I looked at it, It did not run properly due to missing functions in the runtime I think. I will take a look again and see if it is a quick fix. |
Yeah, I just tested this locally by attempting to bypass spin's builtin router and use itty-router directly. You get a stack trace as soon as you call code snippet: import { HandleRequest, HttpRequest, HttpResponse } from "@fermyon/spin-sdk";
import { Router } from "itty-router";
const router = Router();
router.post("/", async req => {
console.log("Parsing body");
let content = req.json();
return {
status: 200,
body: JSON.stringify(content)
}
}
export const handleRequest: HandleRequest = async function (request: HttpRequest): Promise<HttpResponse> {
return await router.handle({
url: request.uri,
method: request.method
});
}; Log output from a POST request:
|
For others, here's a full example showing how to read the request body using the Spin router:
|
@bacongobbler, The error you ran into might be due to the function not existing on the body. There was a bug with the typing fixed by the following But excluding that, I also notice that you are not using the |
I seem to be unable to access the request body when using the router. If I parse the body in
handleRequest
(the commented line), I can access it and everything works well.When I pass the request to the router, I can't access the body anymore.
For example:
This results in:
I am able to pass it as an extra argument:
However, I think it should be part of the request object passed by default.
The text was updated successfully, but these errors were encountered: