V
V
Vladimir Klintsov2022-04-20 12:24:57
JavaScript
Vladimir Klintsov, 2022-04-20 12:24:57

Http-proxy-middleware How to set a header with a forbidden character for a proxy?

It is critical for me that the Content-Type: 'application/json' header goes to the proxy (Content-Type without quotes!) The last thing I tried to do was to change the ContentType key to Content-Type, but js stubbornly changes back to 'Content- Type' { Origin: ' localhost ', 'Content-Type': 'application/json' }

What can you think of?

On the back is a binary, nothing can be changed

const apiProxyHeaders = {
    Origin: 'http://localhost',
    ContentType: 'application/json'
  };
const correctHeader = { ContentType: "Content-Type"};
const apiCorrectHeader = renameKeys(apiProxyHeaders, correctHeader);
console.log(apiCorrectHeader); // { Origin: 'http://localhost', 'Content-Type': 'application/json' }
const apiProxy = createProxyMiddleware({
  target: apiRoot,
  ws: true,
  logLevel: 'debug',
  headers: apiCorrectHeader
});

app.use('/api', apiProxy);


function renameKeys(obj, newKeys) {
  const keyValues = Object.keys(obj).map(key => {
    const newKey = newKeys[key] || key;
    return { [newKey]: obj[key] };
  });
  return Object.assign({}, ...keyValues);
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question