W
Wasp-lang•6mo ago
haarismian

I am getting a 404 with a custom api when using a parameter

this is my wasp api api getPexels { httpRoute: (GET, "/api/get-pexels/:query"), fn: import { getPexels } from "@src/server/pexels.js", } this is my api call const res = await api.get(/api/get-pexels?query=${selectedCity});
and this is my backend function export const getPexels: GetPexels<{ query: string }> = async (req, res, context) => { console.log('pexels called');
2 Replies
kapa.ai
kapa.ai•6mo ago
Hi @haarismian. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- It seems like you are trying to use a query parameter in your API call, but your API is expecting a route parameter. In your Wasp API declaration, you have defined a route parameter :query:
api getPexels {
httpRoute: (GET, "/api/get-pexels/:query"),
fn: import { getPexels } from "@src/server/pexels.js",
}
api getPexels {
httpRoute: (GET, "/api/get-pexels/:query"),
fn: import { getPexels } from "@src/server/pexels.js",
}
However, in your API call, you are using a query parameter ?query=${selectedCity}:
const res = await api.get(`/api/get-pexels?query=${selectedCity}`);
const res = await api.get(`/api/get-pexels?query=${selectedCity}`);
To fix this, you should change your API call to use a route parameter instead of a query parameter. Here's how you can do it:
const res = await api.get(`/api/get-pexels/${selectedCity}`);
const res = await api.get(`/api/get-pexels/${selectedCity}`);
This way, selectedCity will be passed as a route parameter and should be accessible in your backend function as req.params.query. For more information on how to create and use APIs in Wasp, you can refer to the Wasp documentation (https://wasp-lang.dev/docs/advanced/apis). ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ---------------------- Sorry @haarismian, 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?
miho
miho•6mo ago
Kapa is correct 🙂 using :query means you are defining a route parameter and the route expects /api/get-pexels/something
Want results from more Discord servers?
Add your server