0

I am trying to hit a POST API Endpoint with Guzzle in PHP (Wordpress CLI) to calculate shipping cost. The route expects a RAW JSON data in the following format:

{
   "startCountryCode": "CH"
   "endCountryCode": "US",
   "products": {
       "quantity": 1,
       "vid": x         //Variable ID
    }
}

Link to the API I am consuming: https://developers.cjdropshipping.com/api2.0/v1/logistic/freightCalculate

$body = [
     "endCountryCode"    => "US",
     "startCountryCode"  => "CN",
     "products"          => [
             'vid'               => $vid,
             'quantity'          => 1
     ],
 ];

 $request = $this->client->request(
     'POST', 'https://developers.cjdropshipping.com/api2.0/v1/logistic/freightCalculate',
     [
         'headers' => [
                'CJ-Access-Token' => $this->auth_via_cj(), // unnecessary, no auth required. Ignore this header
         ],
         'body' => json_encode( $body )
     ],
);

I've also tried using 'json' => $body instead of the 'body' parameter.

I am getting 400 Bad Request error.

Any ideas?

The Guzzle exception I am getting

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • probably not a solution but definitely, by their docs you must also provide `content-type` header which you are missing: https://developers.cjdropshipping.com/cj/#flow Also I'd rather make my code work with simple `api/account/start` endpoint first. – Marcin Orlowski Feb 04 '22 at 09:39
  • @MarcinOrlowski I've tried explicitly adding the Content Type header, but it didn't help. Btw I might be wrong, but when using the 'json' property Guzzle automatically sets the content-type header. – Hristijan Manasijev Feb 04 '22 at 09:56
  • use try catch to catch the message of 400 bad request error use $e->getMessage();, knowing the error helps to debug faster – bhucho Feb 04 '22 at 10:29
  • @bhucho Here is the error I am getting https://i.imgur.com/SMXWIiP.png – Hristijan Manasijev Feb 04 '22 at 16:01
  • You are getting the error but it is in truncated form try to use try{} catch(RequestException $e){$e->getMessage()} take reference from [this answer](https://stackoverflow.com/a/64603614/9471283) – bhucho Feb 04 '22 at 16:07
  • Yeap, no useful information in it. https://i.imgur.com/uzRd3qA.png – Hristijan Manasijev Feb 04 '22 at 16:27
  • I still couldn't see the message it would be in text form not in html, show your updated code here with catch statement what did you got in $e->getMessage() it should not be in html or try to use `if ($e->getResponse()->getStatusCode() == '400'){ $error['response'] = $e->getResponse()->getBody()->getContents(); }` in catch as you know you are getting 400 error – bhucho Feb 05 '22 at 10:46

3 Answers3

1

Try to give body like this.

"json" =>  json_encode($body)
1

I spent so many hours on this to just realise that products is actually expecting array of objects. I've been sending just a one-dimensional array and that was causing the 'Bad Request' error.

In order to fix this, just encapsulate 'vid' and 'quantity' into an array and voila!

0

You don't need to convert data in json format, Guzzle take care of that. Also you can use post() method of Guzzle library to achieve same result of request. Here is exaple...

$client = new Client();
$params['headers'] = ['Content-Type' => 'application/json'];
$params['json'] = array("endCountryCode" => "US", "startCountryCode" => "CN", "products" => array("vid" => $vid, "quantity" => 1));
$response = $client->post('https://developers.cjdropshipping.com/api2.0/v1/logistic/freightCalculate', $params);
onkaram
  • 560
  • 2
  • 7