How can I access a raw request?
I added a new endpoint (Wasp action). Inside my operation, I want to access a raw request and check the request header value. I couldn't find a way to do this. I tried to access the request from the Wasp context, but it is not there.
This is how I define wasp action:
action incrementStats {
fn: import { incrementStats } from "@src/ai-forms/operations",
entities: [User]
}
Could someone please help me with this?
6 Replies
Hi @lucky72o. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
Wohooo @lucky72o, you just became a Waspeteer level 1!
----------------------
For accessing raw request headers, you need to use Wasp's API feature rather than an action. Actions in Wasp are designed for RPC-style operations and don't provide direct access to the HTTP request.
Here's how you can solve your problem:
Use an API instead of an action
Instead of using an action, you should define an API endpoint which gives you access to the raw request:
Then in your implementation file, you'll have access to the request object:
In TypeScript, you can use the generated types:
The API implementation gives you access to Express's request (
req
) and response (res
) objects, along with Wasp's context
object that contains user session information and entities Advanced APIs.
To call this API from your client code, you can use the Axios wrapper:
This approach gives you full control over the HTTP request and response, including access to headers, while still allowing you to use Wasp's entity system.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------
Sorry @lucky72o, I seem to be having a bit of trouble. Don't worry though, I've pinged my support and they will try to fix me up! :technologist:
Perhaps try again in 15mins?Hey, @lucky72o , the bot is right.
Headers can be accessed in API endpoints. I hope that is okay for your use case.
thanks, I will try to use API endpoints instead of actions 👍
Let me know if all is working okay for you now.