执行向外部API的发布请求时遇到问题

时间:2019-07-10 08:09:22

标签: node.js api post axios

我正在使用axios来执行对外部api的获取和发布请求, 我终于成功实现了get请求(ssl证书的问题,我通过添加以下内容避免了它: httpsAgent:新的https.Agent({{UnknownUnauthorized:false})

现在我想发布api,

为了让请求在邮递员中工作,我输入了 标头content-type:application / json 并在体内:{}

like here

当尝试使用谷歌浏览器扩展时,为了使其正常工作,我没有在标题中放入任何内容,但在参数中,我选择了customer:application / json,然后将其放置在此{}中,而不是默认选择x-www -form-urlencoded; charset = UTF-8

chrome extention

在我的JavaScript应用中,我尝试过

var url = https://10.11.31.100:9440/api/nutanix/v3/images/list;

axios({
    method:'post',
    httpsAgent: new https.Agent({ rejectUnauthorized: false }),
    url,
    auth: {
        username: '******',
        password: '********'
    },
    headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json',
    'X-Requested-With': 'XMLHttpRequest'
        },
params: {},
    data: {}

})
.then(function (response) {
    res.send(JSON.stringify(response.data));
    console.log(response);
})
.catch(function (error) {
    console.log(error);
});

我遇到了这个问题:

TypeError:UTF-8不是函数

1 个答案:

答案 0 :(得分:0)

特别是关于Nutanix v3 REST API-这可能是因为上面的POST请求没有适当的JSON有效负载,即“ data”参数为空。

在发送Nutanix v3 API POST请求时,特别是向“列出”实体发送​​请求时,您需要指定要列出的实体的“种类”。在您的示例中,下面的JSON有效负载将起作用。

{"kind":"image"}

参见此处:https://nutanix.dev/reference/prism_central/v3/api/images/postimageslist

HTH。 :)

相关问题