How send urlencoded in Postman object[ ] and send to JSa via axios

19 Views Asked by At

I need to make a request in js alternative to this one in php

`$url='http://test/api/insert';  
$params=array(  
         array(  
             'manager_id'=>0,
             'name'=> "Name",   
             'surname'=> "Surname",  
             'email' => array("[email protected]", "[email protected]"),   
             'phone' => array("+94949", "+575784"),   
             'fields' => array(    
                 array('id'=>1200,'value'=>"ee"),    
                 array('id'=>633,'value'=>"ff")    
             )   
               
             ),  
           array(  
             'name'=> "Name2",   
             'manager_id'=>2,  
             'phone' => array("+9f4949", "+f575784"), 
             'fields' => array(    
                 array('id'=>1200,'value'=>"fdf"),    
                 array('id'=>633,'value'=>"dfdfd")    
                 )   
             )  
     );  
  $post = array(  
    'apikey' =>"4dfddfdfdfdsfs",  
    'params'=>$params  
 );         
$ch = curl_init();  
curl_setopt($ch, CURLOPT_URL, $url);  
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);  
curl_setopt($ch, CURLOPT_POST, 1);  
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post));  
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);  
$result = json_decode(curl_exec($ch))`

I did this as a request in postman and it doesn't work:

enter image description here

Perhaps I’m passing the wrong format here, how can I pass it by analogy with php.

MY js :

enter image description here

    async function sendCrm() {
    const obj = {
    apikey: "dfdfdfdfdfdf",
    params: [
      {
        manager_id: 0,
        name:Nname,
        surname: "dfdfdfd",
        email: [""],
        phone: ["+434534"],
        fields: [],
      }
    ]
  };

  const post = new URLSearchParams();
  post.append("apikey", "dfdfdfdfdfdf");
  post.append("params", JSON.stringify(obj.params));

  try {
  const postResponse = await axios.post(
    "http://test/api/insert",
    post,
    {
      headers: {
        "Content-Type": "application/x-www-form-urlencoded",
      },
    }
  );

  return postResponse.data;

  } catch (error") {
      console.error(error);
      return new Response(error, { status: 404 });
  } 
}

The question is how can I send this request in the correct format in JS and Postman?

0

There are 0 best solutions below