0

how post data array in link

data post:

{
     "text": "shandiz",
     "location": {
         "type": "Point",
         "coordinates": [
              1,1
          ]
      }
 }

step 1

i coding posting:

let pData = new FormData();
pData.append('text','shandiz');
pData.append('location["type"]','Point');
pData.append('location["coordinates"]','[1,1]');
return this.http.post('https://map.ir/search?',pData);

but error 400 (Bad Request)

step 2

and send curl for testing

<?php

curlPost('https://map.ir/search', [

    'text' => 'shandiz',
    'location["type"]' =>'Point',
    'location["coordinates"]'=>'[1,1]',


]);

function curlPost($url, $data) {
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);// برای خطای https 
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('api-key:42b0dfbdc6174fea96e552e8097fb52f'));
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    $response = curl_exec($ch);
    if (curl_error($ch)) {
        throw new \Exception(curl_error($ch));
    }
    curl_close($ch);

    // return $response;
    var_dump($response);
}
?>

but curl error

how to post data array in step 1 , step 2?

thanks

ashe405
  • 59
  • 1
  • 9

1 Answers1

0

thanks solved by coding

step 1

let pData = new FormData();
pData.append('text','shandiz');
pData.append('location[type]','Point');
pData.append('location[coordinates][]','1');
pData.append('location[coordinates][]','1');
return this.http.post('https://map.ir/search?',pData);

in link by Mr VtoCorleone

and step 2

<?php

$url ='https://map.ir/search?x-api-key=WsLdHK46I5Wfr5xgI0ynjjyiw9Fyhydu';
$fields = array (
  'text' => 'shandiz',
  'location' => 
  array (
    'type' => 'Point',
    'coordinates' => 
    array (
      0 => 1,
      1 => 1,
    ),
  ),
);

//url-ify the data for the POST
$fields_string = http_build_query($fields);

//open connection
$ch = curl_init();

//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, 1);
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);

//execute post
$result = curl_exec($ch);
echo $result;

//close connection
curl_close($ch);

?>

by my friend

ashe405
  • 59
  • 1
  • 9