W
Wasp•7d ago
lucky72o

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
kapa.ai
kapa.ai•7d ago
Hi @lucky72o. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
MEE6
MEE6•7d ago
Wohooo @lucky72o, you just became a Waspeteer level 1!
kapa.ai
kapa.ai•7d ago
---------------------- 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:
api incrementStats {
fn: import { incrementStats } from "@src/ai-forms/apis",
entities: [User],
httpRoute: (POST, "/increment-stats")
}
api incrementStats {
fn: import { incrementStats } from "@src/ai-forms/apis",
entities: [User],
httpRoute: (POST, "/increment-stats")
}
Then in your implementation file, you'll have access to the request object:
export const incrementStats = (req, res, context) => {
// Access request headers
const headerValue = req.headers['your-header-name'];

// Your logic here

// Send response
res.json({ success: true });
};
export const incrementStats = (req, res, context) => {
// Access request headers
const headerValue = req.headers['your-header-name'];

// Your logic here

// Send response
res.json({ success: true });
};
In TypeScript, you can use the generated types:
import { IncrementStats } from "wasp/server/api";

export const incrementStats: IncrementStats = (req, res, context) => {
// Access request headers
const headerValue = req.headers['your-header-name'];

// Your logic here

// Send response
res.json({ success: true });
};
import { IncrementStats } from "wasp/server/api";

export const incrementStats: IncrementStats = (req, res, context) => {
// Access request headers
const headerValue = req.headers['your-header-name'];

// Your logic here

// Send response
res.json({ success: true });
};
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:
import { api } from "wasp/client/api";

// Call your API
const response = await api.post("/increment-stats", { yourData: "here" });
import { api } from "wasp/client/api";

// Call your API
const response = await api.post("/increment-stats", { yourData: "here" });
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?
toticozeni
toticozeni•7d ago
Hey, @lucky72o , the bot is right. Headers can be accessed in API endpoints. I hope that is okay for your use case.
lucky72o
lucky72oOP•7d ago
thanks, I will try to use API endpoints instead of actions 👍
toticozeni
toticozeni•6d ago
Let me know if all is working okay for you now.

Did you find this page helpful?