0

I am using an external api that doesn't allow client side POST request. I can make a POST request using node.js and I am getting my desired response on the server. I am stuck trying to figure out how to get the response from the server into my HTML file.

 const https = require("https");

const data = JSON.stringify({
  key: "value"
});

const options = {
  hostname: "url",
  port: 443,
  path: "/path",
  method: "POST",
  headers: {
    "Content-Type": "application/json",
    "Content-Length": data.length
  }
};

const req = https.request(options, res => {
  console.log(
    `statusCode: ${res.statusCode} statusMessage: ${res.statusMessage}`
  );
  res.setEncoding("utf8");
  res.on("data", chunk => {
    console.log(chunk);
  });
});

req.on("error", error => {
  console.error(error);
});
req.write(data)
req.end();

This is my server.js file, I'm not sure what the next step is to get in a file.

Marc B
  • 58
  • 6
  • Possible duplicate of [Proxy with express.js](https://stackoverflow.com/questions/10435407/proxy-with-express-js) – Raphael Rafatpanah Apr 16 '19 at 15:57
  • You haven't showed us your server so it's hard to help. It looks like you should call this code from a route handler and write the reply (`chunk`) to the route's response stream. – ty. Apr 16 '19 at 23:02

0 Answers0