Worker is changing the POST method to GET

When I try to make a POST request from postman to a Cloudflare worker, the worker treats the request I am making as a GET request and therefore the information I send through POST is also not arriving in the request.
Example:
Client request
const init = {
method: ‘POST’,
headers: headers,
body: content
}
const response = await fetch(‘https://example.com’, init)

Worker Script
async function handleRequest(event) {

return new Response(event.request.method, { status: 200 })
// Show GET method instead POST method

}

addEventListener(‘fetch’, (event) => {
event.respondWith(handleRequest(event));
});

2 Likes

I run your code and I get a POST in the body. Might be related to the apostrophes.

I have the same issue.

My cf-worker code expects POST requests:

if (request.method !== "POST") {
    return new Response("handels POST requests only", {
      status: 405,
    });
  }

And it worked perfectly until recent worker script release. Now I see the following responses in logs (client side, unchanged):

ApiError: Call failed to endpoint POST http:/.../api/save  # <--POST here proves it should be post
Response code: 405
Server error: handels POST requests only

In Cloudflare logs I see that it receives GET requests:

...
"eventTimestamp": 1661974931932,
  "event": {
    "request": {
      "url": "https://.../api/save",
      "method": "GET",
      "headers": {
...

I can replicate it with python:

import requests
requests.post("http://.../api/save)
<Response [405]>

but then I realized I used HTTP url, so fixing the schema resolved it for me:

import requests
requests.post("https://.../api/save)
<Response [200]>

I also tried to use CF Quik Edit and it works with HTTP:

So looks like the issue somewhere in HTTP to HTTPS conversion that happens before the worker receives request. This is visible in my previous message: in CF logs GET HTTPS but the client sends POST HTTP

2 Likes

When you make a POST to the http:// URL, you likely get a 301 response redirecting to the https:// version. When an HTTP client gets a 301 response to a POST request, the client will perform a GET request to the redirect URL. The only 3xx response that preserves the method is 307.

2 Likes

Thank you!!!

Changing the call from http to https was the solution!

1 Like