How to have Cloudflare Worker allow receiving POST data via JS in PHP

I am currently running a Cloudflare Worker to serve a blog on a /subdirectory

This is the code and it works perfectly, thanks to @gregbrimble

addEventListener('fetch', event => {
  event.respondWith(handleRequest(event.request))
})

async function handleRequest(request) {
  const url = new URL(request.url)
  url.hostname = 'blogstatic-blog.bstatic.io'
  url.pathname = url.pathname.replace(/^\/blog(\/)?/, '/')

  const response = await fetch(url)

  return new HTMLRewriter().on('[href]', {
    element(e) {
      let href = e.getAttribute('href')
      if (href.startsWith('/')) {
        href = `/blog${href}`
      } else if (href.startsWith('https://blogstatic-blog.bstatic.io/')) {
        href = href.replace('https://blogstatic-blog.bstatic.io', '/blog')
      }
      e.setAttribute('href', href)
    }
  }).on('[src]', {
    element(e) {
      let src = e.getAttribute('src')
      if (src.startsWith('/')) {
        src = `/blog${src}`
      } else if (src.startsWith('https://blogstatic-blog.bstatic.io/')) {
        src = src.replace('https://blogstatic-blog.bstatic.io', '/blog')
      }
      e.setAttribute('src', src)
    }
  }).transform(response)
  
}

The issue I am running across is that I have an email subscription on the page blogstatic.io/blog that is supposed to receive the POST data from JS, but the PHP is not having it because I don’t have that directive in the worker above (I think!).

Payload is being sent, but PHP is not receiving it and returning an error even when entering a valid email address because it doesn’t have the data, to begin with.

If anyone has any clue on how to go about it, I would greatly appreciate it.

I am looking at this, but not sure if it’s the right solution to look at: Post JSON · Cloudflare Workers docs

This is my progress so far.

The first chunk of code grabs the POST data, and the one below serves to my PHP code…

However, I’m not sure how to dynamically populate my post_data from the first chunk of code

/**
 * readRequestBody reads in the incoming request body
 * Use await readRequestBody(..) in an async function to get the string
 * @param {Request} request the incoming request to read from
 */
async function readRequestBody(request) {
  const { headers } = request;
  const contentType = headers.get('content-type') || '';

  if (contentType.includes('application/json')) {
    return JSON.stringify(await request.json());
  } else if (contentType.includes('application/text')) {
    return request.text();
  } else if (contentType.includes('text/html')) {
    return request.text();
  } else if (contentType.includes('form')) {
    const formData = await request.formData();
    const body = {};
    for (const entry of formData.entries()) {
      body[entry[0]] = entry[1];
    }
    return JSON.stringify(body);
  } else {
    // Perhaps some other type of data was submitted in the form
    // like an image, or some other binary data.
    return 'a file';
  }
}


async function handleRequestOne(request) {
  const reqBody = await readRequestBody(request);
  const retBody = `${reqBody}`;
  return new Response(retBody);
}

addEventListener('fetch', event => {
  const { request } = event;
  return event.respondWith(handleRequestOne(request));
});





/// the code above grabs the incoming POST data
/// the code below serves the POST data to my PHP script

/// ???? How do I dynamically populate ```post_data``` from the code above


post_data = {
  results: ['default data to send'],
  errors: null,
  msg: 'I sent this to the fetch',
};

/**
 * Example someHost is set up to take in a JSON request
 * Replace url with the host you wish to send requests to
 * @param {string} url the URL to send the request to
 * @param {BodyInit} body the JSON data to send in the request
 */
const someHost = 'https://blogstatic-blog.bstatic.io/internal/subscribe';
const url = someHost;
const body = post_data;

/**
 * gatherResponse awaits and returns a response body as a string.
 * Use await gatherResponse(..) in an async function to get the response body
 * @param {Response} response
 */
async function gatherResponse(response) {
  const { headers } = response;
  const contentType = headers.get('content-type') || '';
  if (contentType.includes('application/json')) {
    return JSON.stringify(await response.json());
  } else if (contentType.includes('application/text')) {
    return response.text();
  } else if (contentType.includes('text/html')) {
    return response.text();
  } else {
    return response.text();
  }
}

async function handleRequest() {
  const init = {
    body: JSON.stringify(body),
    method: 'POST',
    headers: {
      'content-type': 'application/json;charset=UTF-8',
    },
  };
  const response = await fetch(url, init);
  const results = await gatherResponse(response);
  return new Response(results, init);
}
addEventListener('fetch', event => {
  return event.respondWith(handleRequest());
});

Found this on StackO and it works perfectly: javascript - Forward body from request to another url - Stack Overflow