【问题标题】:Nodejs Receiving errro "options.hostname" property must be one of type string, undefined, or null. Received type object at validateHostNodejs 接收错误的“options.hostname”属性必须是 string、undefined 或 null 类型之一。在 validateHost 接收到类型对象
【发布时间】:2019-03-07 21:41:16
【问题描述】:

我正在尝试发布请求以打开 permID API。

请求地址为https://api.thomsonreuters.com/permid/match/file?Content-Type=multipart%2Fform-data

我尝试从头开始编写代码,并从邮递员生成的代码中复制和过去的工作参数、标题和正文值。

我写的代码被注释掉了。

不知道哪里出错了。

谢谢。

const express = require('express')
const app = express()


const request = require('request');
const cors = require('cors');
// const http = require('http');
// const https = require('https');
const util = require('util')


// Sets an initial port. We"ll use this later in our listener
const PORT = process.env.PORT || 8080;


app.use(cors());

var corsOptions = {
  origin: '*',
  optionsSuccessStatus: 200
};

// Service end-point (search) 

// app.get('/upload', function (req, res) {

//   var options = {
//     method: 'GET',
//     url: 'https://api.thomsonreuters.com/permid/match/file?Content-Type=multipart%2Fform-data',
//     headers: {
//       'cache-control': 'no-cache',
//       'x-openmatch-dataType': 'Organization',
//       'x-openmatch-numberOfMatchesPerRecord': '1',
//       'x-ag-access-token': '96CT8NAgnieeuiYA2YPeNMnbPMfHu4W8'
//     },
//     formData: {
//       file: undefined
//     }
//   };

//   request(options, function (error, response, body) {
//     if (error) throw new Error(error);
//     console.log(response)
//     console.log(body);
//   });

// });

// function dataReturned(e) {
//   console.log("Success!");
//   finalRes.end(e);
// }

// const server = http.createServer(app)
// server.listen(PORT, () => {
//   console.log(`app started! at port ${PORT}`);
// });
var http = require("https");

var options = {
  "method": "POST",
  "hostname": [
    "api",
    "thomsonreuters",
    "com"
  ],
  "path": [
    "permid",
    "match",
    "file"
  ],
  "headers": {
    "content-type": "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW",
    "x-openmatch-numberOfMatchesPerRecord": "1",
    "x-openmatch-dataType": "Organization",
    "cache-control": "no-cache",
    "Postman-Token": "101e3dbe-96bf-4b1c-9fce-8dad5f4ff975"
  }
};

var req = http.request(options, function(res) {
  var chunks = [];

  res.on("data", function(chunk) {
    chunks.push(chunk);
  });

  res.on("end", function() {
    var body = Buffer.concat(chunks);
    console.log(body.toString());
  });
});

req.write("------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"file\"\r\n\r\n\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--");
req.end();

【问题讨论】:

  • 错误似乎很明显。您的 options.hostname 是一个数组,而不是所需的字符串。

标签: node.js server


【解决方案1】:

基于HTTPS documentation,看起来hostname 应该是一个字符串而不是一个数组。

而不是

var options = {
  "method": "POST",
  "hostname": [
    "api",
    "thomsonreuters",
    "com"
  ],
...

试试

var options = {
  "method": "POST",
  "hostname": "api.thomsonreuters.com",
...
``

【讨论】:

  • 更新后我在终端 $ node index.js Error

    发生错误。

    抱歉,您要查找的页面目前不可用。
    请稍后再试。

  • 我相信您需要对options.path 进行类似的更改 - 它是一个字符串,而不是字符串数组
  • 这并没有改变我收到的结果。参考原始代码+更改主机名和字符串路径,我很好奇将文件上传到api可能是问题所在。查看代码我没有看到文件附件的路径,但我不确定我是否缺少某些东西,因为邮递员在示例代码中处理了它。
猜你喜欢
  • 2020-12-09
  • 2019-10-13
  • 2020-11-02
  • 1970-01-01
  • 2020-02-06
  • 2020-06-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多