节点Axios模块PUT请求具有自定义HTTP标头和数据

时间:2017-12-25 22:05:11

标签: node.js axios put

我想向创建ElasticSearch索引的端点发送PUT请求。使用Axios NPM模块。如何使用数据和自定义标头发送不同REST动词的请求。

const options = {
hostname: "c1-es03c.aue2l.internal",
port: 9200,
path: "/",
method: "PUT",
rejectUnauthorized: true,
requestCert: true,
servername: "c1-elasticsearch.internal",
ca: [fs.readFileSync("./Root-CA.pem")],
headers: {
"Authorization": "Basic xxxxx"
}
req.write("{data here}");
req.end();
var req = https.request(options, callback)

HTTPS模块工作正常,我如何对axios或请求做同样的事情。

axios.put(网址,选项,数据)没有用。

1 个答案:

答案 0 :(得分:0)

您可以使用  $ npm install axios'安装axios。命令。您可以通过此打字稿代码轻松理解。有关更多信息,您可以使用此链接, Axios npm

const axios = require('axios');

export class RequestContext {

    constructor() { }

    post(request: any, response: any): void {

        let header = {
            "Content-type": "application/json"
        }
        let data = request.data;
        axios.put(url, data, header).then((result) => {
        \\ .then() methord is use when you want to process on data that return from API.
            console.log(result)
        })
    }
}