POST API endpoint only working if request made from POSTMAN

I am developing an app which needs to send information to an endpoint another client has put online to receive POST requests. This is just a simple POST request I send with a body, then the client saves its body to wherever he wants to. This endpoint has no Authorization header as it has been removed recently due to thinking it might be the cause for it not to work. So, now, it only works if you do the request from Postman. I created a simple Expressjs server and deployed on Vercel to mock that client endpoint for testing purposes (just returns the req.body to the client) I deployed my main project on Vercel and made POST requests to my mock Express app. Works. I deployed my main project on AWS Amplify and made POST requests to my mock Express app. Works. But when I try to attack the original endpoint URL the client has put online, it just does not work. It returns a 404 status everytime. But not from Postman (200 status). I just don't get it... maybe I am malforming the requests? I just want to be really sure the issues is not on my end. My code is part of a create-t3-app stack that uses tRPC first to fetch information from my PlanetScale DB and then makes a POST request to that endpoint:
23 Replies
ray1sx
ray1sxOP17mo ago
export const viveRouter = createTRPCRouter({
findUserInsights: publicProcedure
.input(z.object({ ig_account_id: z.string() }))
.mutation(async ({ ctx, input }) => {
const influInfo = await ctx.prisma.instagramStats.findFirst({
where: {
ig_account_id: input.ig_account_id,
},
select: {
ig_account: true,
followers_count: true,
audience_city: true,
audience_country: true,
audience_gender_age: true,
audience_locale: true,
},
});
await axios
.post($env.URL_ENDPOINT, influInfo, {
headers: {
Accept: "application/json",
"Content-Type": "application/json",
// Authorization: `Bearer ${env.V_TOKEN}`,
},
})
.then(function (response) {
console.log(`res status ${response.status}:`, response.status);
})
.catch(function (error) {
console.error(error.message)

});

}),
});
export const viveRouter = createTRPCRouter({
findUserInsights: publicProcedure
.input(z.object({ ig_account_id: z.string() }))
.mutation(async ({ ctx, input }) => {
const influInfo = await ctx.prisma.instagramStats.findFirst({
where: {
ig_account_id: input.ig_account_id,
},
select: {
ig_account: true,
followers_count: true,
audience_city: true,
audience_country: true,
audience_gender_age: true,
audience_locale: true,
},
});
await axios
.post($env.URL_ENDPOINT, influInfo, {
headers: {
Accept: "application/json",
"Content-Type": "application/json",
// Authorization: `Bearer ${env.V_TOKEN}`,
},
})
.then(function (response) {
console.log(`res status ${response.status}:`, response.status);
})
.catch(function (error) {
console.error(error.message)

});

}),
});
@stoykotlv
stoyko
stoyko17mo ago
taking a look
ray1sx
ray1sxOP17mo ago
After all the debugging and trial/error I did I just think the client has the issue on how he has configured that endpoint, but maybe I am missing something on my end that I just can't see. But logically if code works on any endpoint I try but not on the one the client has set up... thinkies
ray1sx
ray1sxOP17mo ago
fyi. logs from aws amplify
stoyko
stoyko17mo ago
Okay, so you have a TRPC query that you run, which first gets data from the DB, then you make a POST request to a 3rd party API with the response from the DB. Can you log the URL to which you are posting the request? Might be something with the URL itself?
ray1sx
ray1sxOP17mo ago
Yes, that is correct. The url itself it is not the issue, I tried hardcoding it to the code and it has the same problems.
stoyko
stoyko17mo ago
Cuz if it works from everywhere else, but your trpc procedure, it won't be something from the client API
ray1sx
ray1sxOP17mo ago
I mean, my tRPC procedure works for any url I try except the client API url
stoyko
stoyko17mo ago
And does the client API work if you do a curl reqeust or via postman directly?
ray1sx
ray1sxOP17mo ago
Yes, it does
stoyko
stoyko17mo ago
Then the issue shouldn't be in the client API. Can you try removing the headers and see if that will work?
ray1sx
ray1sxOP17mo ago
I am fetching the log, one moment Same error without headers:
2023-07-11T08:30:32.900+02:00 status: 404,
2023-07-11T08:30:32.900+02:00 statusText: 'Not Found',
2023-07-11T08:30:32.900+02:00 status: 404,
2023-07-11T08:30:32.900+02:00 statusText: 'Not Found',
It doesnt make sense, I just can't understand what is happening
stoyko
stoyko17mo ago
Where are you deploying this app?
ray1sx
ray1sxOP17mo ago
vercel / aws
stoyko
stoyko17mo ago
Well, it isn't much, but can you try switching to a try/catch block, as it might be resolving differently, thus causing these requests in .then() not working
ray1sx
ray1sxOP17mo ago
I switched from try/catch to .then() thinking the same the other way around. The thing is the entire code works because when I post request to my other server it succeeds just not on the client url
stoyko
stoyko17mo ago
I am repeating myself here, but are you sure you are hitting the correct URL?
ray1sx
ray1sxOP17mo ago
but then again it works on postman yes.. same url works on postman, fails on the code as I can see it is not something trivial I am missing on my code (it was what I was afraid of)
stoyko
stoyko17mo ago
Have you tried doing a simple cURL request to the same URL? Just to see if it will give you the same error. It might be some header that Postman is adding, which you are missing here Only other thing I can think of is setting up a repo where I can reproduce this, to see if I can debug it further.
ray1sx
ray1sxOP17mo ago
I appreciate it but there is no need... this exact code is working when doing get/post requests to facebook API
stoyko
stoyko17mo ago
no worries. Good luck troubleshooting further.
ray1sx
ray1sxOP17mo ago
I've done it and the server responds well. I will go back on everything we've discussed step by step and make sure the app itself os not modifying the url on the fly (which I made sure it didn't) In any case thank you for your effort and time!
stoyko
stoyko17mo ago
Good luck!
Want results from more Discord servers?
Add your server