【发布时间】: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 是一个数组,而不是所需的字符串。