简体   繁体   中英

How can I make an HTTPS POST request in node.js?

How can I make an HTTPS POST request to an API. My code doesn't work, because it cannot find the resource.

API Documentation: https://applymagicsauce.com/documentation at Authentication

My code, which doesn't work

var https = require('https');

var options = {
  host: 'https://api.applymagicsauce.com',
  port: 443,
  path: '/auth',
  method: 'POST'
};

var req = https.request(options, function(res) {
  console.log('STATUS: ' + res.statusCode);
  console.log('HEADERS: ' + JSON.stringify(res.headers));
  res.setEncoding('utf8');
  res.on('data', function (chunk) {
    console.log('BODY: ' + chunk);
  });
});

req.on('error', function(e) {
  console.log('problem with request: ' + e.message);
});

// write data to request body
req.write('data\n');
req.write('data\n');
req.end();

error message: STATUS: 415 HEADERS: {"server":"nginx/1.15.10","date":"Fri, 09 Aug 2019 18:22:48 GMT","content-type":"application/json;charset=UTF-8","transfer-encoding":"chunked","connection":"close","x-content-type-options":"nosniff","x-xss-protection":"1; mode=block","cache-control":"no-cache, no-store, max-age=0, must-revalidate","pragma":"no-cache","expires":"0","strict-transport-security":"max-age=15724800; includeSubDomains","x-frame-options":"DENY"} BODY: {"timestamp":"2019-08-09T18:22:48.981+0000","status":415,"error":"Unsupported Media Type","message":"Content type 'application/octet-stream' not supported","path":"/auth"}

看起来您的主机包含一条路径,但您也正在提供一条路径。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM