How to proxy POST Request in Netlify?

I’m trying to call my API (netlify function hosted in the same team) using POST. At first I use status code 200 for the proxy, but it just returns the request with no response and no x-nf-request-id. Then, I tried to omit the status code, leaving the redirect code only this:

[[redirects]]
  from = "/api/*"
  to = "https://deploy-preview-2--energy-monitoring-backend-itb-ac-id.netlify.app/.netlify/functions/setup-server/:splat"
  force = true
  headers = {Access-Control-Allow-Origin = "*", Access-Control-Allow-Methods = "POST"}

The Location returns the right URL ( x-nf-request-id: aa60936f-5ce8-41d9-962d-97152a19479a-14525254 ) , but after this my POST Request becomes GET Request ( x-nf-request-id: 5c8154ab-6c92-420a-bd18-29a4cde20776-8610475 ), which makes it fails as my API only accepts POST.

If I tried to use status code 307/308 to maintain POST request, Netlify would redirect me to 404 Not Found ( x-nf-request-id: e0c2e38a-f01d-41ec-96e6-67e297111830-10026811 and e0c2e38a-f01d-41ec-96e6-67e297111830-10023464 ).

How to proxy this POST Request? Do I need to use netlify functions? I also need to change the origin so I can send cookies to the API. How?

Site: https://deploy-preview-1--energy-monitoring-itb-ac-id.netlify.app

Hey there, @darkGrimoire :wave:

Welcome to the Netlify Forums. Thanks for writing such a detailed post about this. I have closed the other threads that you opened (here and here) so that we can focus the conversation in one place. This will ensure that individuals to not respond to multiple posts with similar or the same information.

Have you taken a look at our redirects debugging guide as well as our Docs? These would be good places to start if you have not :slight_smile:

Hey @darkGrimoire

Did you find solution to the problem of POST APIs ?

I haven’t found solution yet, and we are undergoing backend migration so I haven’t take a closer look at this issue. The error it gives is Network Error, and I think the problem is in our backend so I’ll try to look into our system first.

1 Like

Hey there, @darkGrimoire :wave:

I just wanted to follow up, as this thread has been a bit quiet recently. Did the error end up being a backend error in your system, or is this still unresolved?

Let us know at your earliest convenience.

it is solved. Actually, I don’t know where the error originates from because after we migrates our backend to azure it suddenly works straight away. Maybe there’s a problem when proxying to netlify-lambda? Well anyway, it works now. Thank you for all of your help!

1 Like

That is great, I am so glad this is resolved for you! :netliconfetti:

I am curious if there is some more guidance with this same issue. It looks like the OP went a different route about a year ago, please tell me it’s possible to do this now.

Hi @emptydub

I’ve run a little test posting via a proxy to an online request checker, using fetch and POST requests work fine.

netlify.toml

[[redirects]]
  from = "/api/postreq"
  to = "https://www.example.com/postpoint"
  status = 200

then the JavaScript

const response = await fetch("/api/postreq", {
  method: "POST",
  headers: {
    "Content-Type": "application/json",
  },
  body: JSON.stringify({
    "some": "Key",
    "and": "Another"
  })
})

Have you tried something that didn’t work?

1 Like

I’m using Nextjs and axios for my requests(ik that prob doesn’t matter much). My solution is a 2 stage request. Setup: my form is embedded into a /quotes page. I would like to use textfield data that is gathered on the client → invoke a netlify lambda function, where that will take that json data and some .env vars for the required headers (facilitates request) → posts via axios to the api endpoint. On success/fail I also need that response to be passed back to the frontend. This is all due to keeping the keys away from the client. When I request this proxied call from the frontend, in Nextjs my response is some html that resembles the 1st of the 2 request stages with absolutely no data or indication that the netlify function fired at all. I can’t seem to find much about the issue I am having - or rather if I am doing it correctly (thinking about it right).

Hey @emptydub,

If you can share a reproduction case, that can help us better.