0
votes

I'm using axios to perform get and post requests to an external api, i have finally succeed to achieve get request (problem with the ssl certificate, i avoid it by adding this : httpsAgent: new https.Agent({ rejectUnauthorized: false }),

now i would like to post the api,

to get the request working in postman, i put in headers content-type : application/json and in the body : {}

like here

when trying with a google chrome extention, to make it work, i put nothing in the headers but in params, i select customer : application/json and i put inside this {} instead of the default choice which is x-www-form-urlencoded;charset=UTF-8

chrome extention

in my javascript app i tried this

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);
});

I get this problem :

TypeError : UTF-8 is not a function

1
The TypeError : UTF-8 is not a function is printed by the .catch()? What's the point of the X-Requested-With': 'XMLHttpRequest header? This code seems ok to me. - Andrea Franchini
yes the error is hadled by the catch promise - Aymene
'X-Requested-With': 'XMLHttpRequest' has no point i just tried it - Aymene
don't you think there is a problem with axios supporting node.js post requests ? - Aymene
I don't think the problem is on axios itself. Seems that somewhere you're trying to use a string like a function: const randomName = 'foobar' => randomName() - Andrea Franchini

1 Answers

0
votes

Specifically with regards to the Nutanix v3 REST API - this is probably because the POST request above doesn't have an appropriate JSON payload i.e. the "data" parameter is empty.

When sending Nutanix v3 API POST requests, specifically to "list" entities, you'll need to specify the "kind" of entity being listed. In your example, the JSON payload below will work.

{"kind":"image"}

See here: https://nutanix.dev/reference/prism_central/v3/api/images/postimageslist

HTH. :)