Does using the redirects route to a function call change the method of the request from POST to GET?

I had this issue where I was sending a request to a netlify function and instead of using the /.netlify/functions/functionName.js I was using the /api/functionName.js route. this seemed to work fine for a GET request but when I sent a POST request the httpMethod was getting changed to GET.

But when I changed the route to /.netlify/functions/functionName.js the method was being read properly as POST. Is this expected?

I had put this up on stackoverflow (javascript - How to define Netlify function endpoint as POST? - Stack Overflow) and wanted to confirm if my findings were right before I accept my own answer as the right answer (that basically calling a function on the /api/ route changes the method from POST to GET)

Some advise on this would be appreciated.

Hey @alimbolar

I have never had an issue with a POST going through as a GET when using a rewrite.

Do you have something like the following in a _redirects file

/api/*     /.netlify/functions/:splat   200

or like the following in a netlify.toml file

[[redirects]]
  from = "/api/*"
  to = "/.netlify/functions/:splat"
  status = 200

Hi @coelmay

I have this in my netlify.toml … it’s similar to what you shared but not exact… I had copied it the way it was specified from some documentation…

[[redirects]]
from="/api/*"
to="/.netlify/functions/:splat"

My apologies @alimbolar, my error (which I have corrected).

Yes, what you have is correct. But you should add the status = 200 line as well. If you don’t, it defaults to a 301 and then the method will change. So what you need is

[[redirects]]
from="/api/*"
to="/.netlify/functions/:splat"
status = 200
1 Like

Thanks for the update @coelmay … really appreciate it…

So just to reconfirm… by adding the status as 200, do I ensure that the GET request will stay as a GET and POST request will stay as a POST?

It’s a bit strange that the redirect has a common status defined before the request is sent to the server/function and that the status is not a response of the server/function, no? Is there something I am not understanding here?

Or is it that by defining the status one can actually define the verb of the function call? ie by defining the status as ‘200’ one defines it as a POST request and by defining it as a ‘301’ one defines it as a GET.

Would appreciate if you could help me clear up the confusion a little bit.

A 200 status code passes the request along to the proxied endpoint, and returns the response, such that if the endpoint returns a 500 Internal Server Error, or 405 Method Not Allowed, that is returned to the code that made the original request.

A 301 Moved Permanently redirects to the endpoint, much like a short URL service like Bitly works. By default, Netlify uses a 301 status code for rewrite.

3 Likes

Thanks @coelmay … I tested with your setting and it works fine… so I intend to always add a status=200 irrespective of whether it’s a GET or POST… it seems to work fine this way and I just wanted to confirm if that’s the right way…

But if that’s the case then I guess the documentation should suggest that one should always add the status in the redirects section in the netlify.toml… no? Is there a reason/need when one should not put the status?

You might need/want to temporarily or permanently redirect a path on your site e.g. /blog to /news so you wouldn’t want to use a 200 rewrite rather a 301/302 e.g.

/blog/*   /news/:splat   301

or may wish to redirect /blog to a subdomain e.g.

/blog/*   https://blog.example.com/:splat   301

It does already. In the linked documentation it says under the 200: Ok code

This is used for rewrites and proxying.

2 Likes

Got it…:-)… thanks for the clear and detailed explanation. Really appreciate it.

1 Like