N
N
Nikens2020-05-07 13:18:56
PHP
Nikens, 2020-05-07 13:18:56

How to make an online calculation through the TC Energy API?

It is not possible to carry out an online calculation of the shipment through the API ( apidoc.nrg-tk.ru/v3/ru ) An
error response is received

stdClass Object
(
    [code] => InvalidJson
    [message] => Invalid json
    [extraInfo] => stdClass Object
        (
            [fieldMustBeType] => []api.CargoItem
            [fieldValue] => object
        )
)

Something with the data type, although everything seems to be according to the description. The original request code:
$data = array(
        "idCityFrom" => (int)$kem,
        "idCityTo" => (int)$code,
        "cover" => 0,
        "idCurrency" => 0,
        "items" =>["weight"=>10.2,"width"=>0.39,"height"=>0.39,"length"=>0.54],
        "declaredCargoPrice" => 0,
        "idClient" => 0
      );
      $json = json_encode($data);
      $url = ('https://mainapi.nrg-tk.ru/v3/price');
      $ch = curl_init($url);
      curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Accept: application/json", "NrgApi-DevToken: Token"));
      curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
      curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
      $result = curl_exec($ch);
      $obj = json_decode($result);


Either there is a problem with the syntax for forming parameters, or with something else. If you send a request through swagger, then everything is fine, the calculation passes

{
  "idCityFrom": 3842,
  "idCityTo": 343,
  "cover": 0,
  "idCurrency": 0,
  "items": [
    {
      "weight": 10.0,
      "width": 0.39,
      "height": 0.39,
      "length": 0.54
    }
  ],
  "declaredCargoPrice": 0,
  "idClient": 0
}

curl -X POST --header 'Content-Type: application/json' --header 'Accept: application/json' --header 'NrgApi-DevToken: token' -d '{ \ 
   "idCityFrom": 3842, \ 
   "idCityTo": 343, \ 
   "cover": 0, \ 
   "idCurrency": 0, \ 
   "items": [ \ 
     { \ 
       "weight": 10.0, \ 
       "width": 0.39, \ 
       "height": 0.39, \ 
       "length": 0.54 \ 
     } \ 
   ], \ 
   "declaredCargoPrice": 0, \ 
   "idClient": 0 \ 
 }' 'https://mainapi.nrg-tk.ru/v3/price'


Tell me, please, did anyone have to work with them? What is the reason for the error?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ilya, 2020-05-07
@Nikens

I forgot one more square brackets in the items array element

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question