T
TanStack3y ago
deep-jade

Request does not work correctly

Hey. I am using ky and react query. Locally in my Docker container. Everything works fine. My backend gets the request and processes it. As soon as my Webinterface is in a docker container on my server and online....This one endpoint does not work anymore. I have about 15 endpoints.....1 of them (its always the same one) does not work correctly if the webinterface is online. If I send my request via PostMan it does work without any problems (also with the same JSON).
const methods = useForm()
const {serverId} = useParams()
const servers: Server[] = useOutletContext();
const supportOption = useMutation((data) => insertSupportOption(serverId ?? "0", data))
const matchedServer = servers.find(server => server.id === serverId);


const {handleSubmit} = methods
const onSubmit = (data: any) => {

supportOption.mutate(data)
//window.location.href = `/dashboard/${serverId}/support`
}
const methods = useForm()
const {serverId} = useParams()
const servers: Server[] = useOutletContext();
const supportOption = useMutation((data) => insertSupportOption(serverId ?? "0", data))
const matchedServer = servers.find(server => server.id === serverId);


const {handleSubmit} = methods
const onSubmit = (data: any) => {

supportOption.mutate(data)
//window.location.href = `/dashboard/${serverId}/support`
}
My onSubmit does work without any problem...The body is also the right one.
export async function insertSupportOption(serverId: string, body: any) {
console.log(body)
return api.post(`${ORIGIN}/support/options/add`, {
headers: {
'X-Server': serverId
},
json: body
}).formData
}
export async function insertSupportOption(serverId: string, body: any) {
console.log(body)
return api.post(`${ORIGIN}/support/options/add`, {
headers: {
'X-Server': serverId
},
json: body
}).formData
}
This is how my insertSupportOption works
const ORIGIN = "/api/discord"

const api = ky.extend({
hooks: {
beforeRequest: [request => {
request.headers.set('X-XSRF-TOKEN', Cookies.get('XSRF-TOKEN') ?? "0")
}],

afterResponse: [
async (request, options, response) => {
if (response.status === 403 || response.status === 401 || response.status === 400 || response.status === 404) {
window.location.replace("/dashboard");
}
}
]
}
})
const ORIGIN = "/api/discord"

const api = ky.extend({
hooks: {
beforeRequest: [request => {
request.headers.set('X-XSRF-TOKEN', Cookies.get('XSRF-TOKEN') ?? "0")
}],

afterResponse: [
async (request, options, response) => {
if (response.status === 403 || response.status === 401 || response.status === 400 || response.status === 404) {
window.location.replace("/dashboard");
}
}
]
}
})
This is my api variable. Do you guys see anything why this one endpoint does not work only if it is online?
14 Replies
deep-jade
deep-jadeOP3y ago
This is my API framework
deep-jade
deep-jadeOP3y ago
GitHub
GitHub - sindresorhus/ky: 🌳 Tiny & elegant JavaScript HTTP client b...
🌳 Tiny & elegant JavaScript HTTP client based on the browser Fetch API - GitHub - sindresorhus/ky: 🌳 Tiny & elegant JavaScript HTTP client based on the browser Fetch API
rising-crimson
rising-crimson3y ago
What error are you exactly getting?
deep-jade
deep-jadeOP3y ago
No error THATS the problem There is no error I have a console log in my backend which should print after the backend gets a request But it does not So my Frontend does not send anything
exotic-emerald
exotic-emerald3y ago
If you look at the query in the network tab of the devtools, what's the difference with the one that works in postman?
deep-jade
deep-jadeOP3y ago
Fun fact. I don’t see a query which is going out. I only see the response on my local Webinterface. wait let me take a look again
deep-jade
deep-jadeOP3y ago
This is the response on my online webinterface
No description
deep-jade
deep-jadeOP3y ago
Sry its german.
deep-jade
deep-jadeOP3y ago
This is how it should look like
No description
deep-jade
deep-jadeOP3y ago
My backend is not responding on the online webinterface. But if I send a request manually it does respond.....hm
exotic-emerald
exotic-emerald3y ago
CORS issue maybe?
deep-jade
deep-jadeOP3y ago
But why should CORS make a problem on only one endpoint? The other endpoints work well And they are built the same Is it normal that it is not shown that this is a POST on my online version? on my local version it is shown in pink The cookie is also missing CORS would kill all endpoints wouldnt it? Every endpoint works well except this one...And my server is not responding in my network tab. With Postman it is reachable
exotic-emerald
exotic-emerald3y ago
not sure. You can also try sending the request using fetch() directly from the console to see if it works. Is there any error in the browser's console? Does your server receive anything (is there any log)?
deep-jade
deep-jadeOP3y ago
I only have a println in my backend so it should send something into the console if that endpoint gets triggered... Uhm how do i send such a request using fetch in the console? It just told me that there is no cookie If you compare both requests you can see that the first one on my online Webinterface does send a completely different request than my local version (2nd one)

Did you find this page helpful?