Click here to Skip to main content
15,125,299 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Main question => How to post/write the array on a right way with NodeJs http request? (or other way)

The PHP API request array as input. Based on the example it is also need to be converted by http_build_query. The http_build_query PHP function create a following sting: “order_ids%5B0%5D=123&order_ids%5B1%5D=456&order_ids%5B2%5D=789”


PHP
$curl_parameters['order_ids'] = array(123,456,789);

$curl_options = array(
    ...
    CURLOPT_POSTFIELDS => http_build_query($curl_parameters),
    ...
  );



I try to do the same in NodeJs but some reason I can not send the array on a right way therefore I always got the following error message:
"
//error=> implode(): Invalid arguments passed.{"result":"success","event":"getawb","msg":[]}
"

What I have tried:

const order_ids = { "order_ids": [123,456,789] }  
const order = httpBuildQuery(order_ids)   //This line create a same format like in PHP script. It is an NPM package.

const options = {
    mehod: 'POST',                                             
    auth: username + ":" + password,
  };
const req = http.request(url, options, (res) => {                   
    
    res.on('data', (d) => {                                 
        process.stdout.write(d);
      });                                           

  req.write(order);  
  req.end();
};
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900