proxyrequestcorshttp-status-code-500cors-anywhere

PUT requests with CORS-anywhere


I am using cors-anywhere as a proxy server of sorts to overcome some cors problems I was having. My project only requires GET and PUT requests. cors-anywhere solved all my cors and GET problems but now with PUT I am getting 500 error. Below is how I set up my server and make the PUT request:

//setting up server
var host = process.env.HOST || '0.0.0.0';
var cors_proxy = require('cors-anywhere');
cors_proxy.createServer({
    originWhitelist: [], // Allow all origins
    removeHeaders: ['cookie', 'cookie2']
}).listen(8000, host, function() {
    console.log('Running CORS Anywhere on ' + host + ':' + 8000);
});

//making call
let result = await fetch(`http://localhost:8000/${apiUrl}`, {
   method: 'PUT",
   mode: 'cors',
   body: JSON.stringify({"value":"true"})
});
let json = await result.json();
console.log(json);

Everything here works with GET requests and gives a 500 error with PUT request when done through my TypeScript and JavaScript projects. If I use Postman with the same URLs, everything works perfectly. Any ideas on what is going on?

Thank you for your help.


Solution

  • Just figured out the problem. I wasn't declaring the content type in the PUT fetch request header. Correct one looks like this:

    let result = await fetch(`http://localhost:8000/${apiUrl}`, {
       method: 'PUT",
       mode: 'cors',
       headers: {
          'Content-Type': 'application/json'
       },
       body: JSON.stringify({"value":"true"})
    });