3
votes

I have postman request w/c working well.

Below is my post url and headers enter image description here

Then here is my content body enter image description here

It works well when I click Send button it returns correct resource, however, when I am trying to do it in PHP using guzzlehttp/guzzle it returns 422 or 400

My code

$res = $client->request('POST', 'https://api.bigcommerce.com/stores/ny813bjwdy/v3/carts', [
    'headers' => [
        'Accept' => 'application/json',
        'Content-Type' => 'application/json',
        'X-Auth-Client' => "mzr2qe4qeweqwe", 
        'X-Auth-Token' => "nokrq2131qweqrqrqew"
    ],
    'form_params' => [
        'customer_id' => 1,
        'line_items' => [
            'quantity' => 1,
            'product_id' => 97,
            'list_price' => 200
        ]
    ]
]);

echo "<pre>";
    print_r($res);
echo "</pre>";

Fatal error: Uncaught GuzzleHttp\Exception\ClientException: Client error: POST https://api.bigcommerce.com/stores/ny813bjwdy/v3/carts resulted in a 400 Bad Request response: {"status":400,"title":"Input is invalid","type":"https://developer.bigcommerce.com/api#api-status-codes","detail":"Synta (truncated...) in C:\www\bomb-shelter\http\vendor\guzzlehttp\guzzle\src\Exception\RequestException.php:113 Stack trace: #0 C:\www\bomb-shelter\http\vendor\guzzlehttp\guzzle\src\Middleware.php(66): GuzzleHttp\Exception\RequestException::create(Object(GuzzleHttp\Psr7\Request), Object(GuzzleHttp\Psr7\Response)) #1 C:\www\bomb-shelter\http\vendor\guzzlehttp\promises\src\Promise.php(203): GuzzleHttp\Middleware::GuzzleHttp{closure}(Object(GuzzleHttp\Psr7\Response))

2 C:\www\bomb-shelter\http\vendor\guzzlehttp\promises\src\Promise.php(156):

GuzzleHttp\Promise\Promise::callHandler(1, Object(GuzzleHttp\Psr7\Response), Array) #3 C:\www\bomb-shelter\http\vendor\guzzlehttp\promises\src\TaskQueue.php(47): GuzzleHttp\Promise\Promise::Guzzl in C:\www\bomb-shelter\http\vendor\guzzlehttp\guzzle\src\Exception\RequestException.php on line 113

I also tried GuzzleHttp\RequestOptions::JSON

$res = $client->request('POST', 'https://api.bigcommerce.com/stores/ny813bjwdy/v3/carts', [
    'headers' => [
        'Accept' => 'application/json',
        'Content-Type' => 'application/json',
        'X-Auth-Client' => "mzr2qe4qeweqwe", 
        'X-Auth-Token' => "nokrq2131qweqrqrqew"
    ],
    GuzzleHttp\RequestOptions::JSON => [
        'customer_id' => 1,
        'line_items' => [
            'quantity' => 1,
            'product_id' => 97,
            'list_price' => 200
        ]
    ]
]);

echo "<pre>";
    print_r($res);
echo "</pre>";

but it returns 422

Fatal error: Uncaught GuzzleHttp\Exception\ClientException: Client error: POST https://api.bigcommerce.com/stores/ny813bjwdy/v3/carts resulted in a 422 Unprocessable Entity response: {"status":422,"title":"Missing or incorrect required fields","type":"https://developer.bigcommerce.com/api#api-status-co (truncated...) in C:\www\bomb-shelter\http\vendor\guzzlehttp\guzzle\src\Exception\RequestException.php:113 Stack trace: #0 C:\www\bomb-shelter\http\vendor\guzzlehttp\guzzle\src\Middleware.php(66): GuzzleHttp\Exception\RequestException::create(Object(GuzzleHttp\Psr7\Request), Object(GuzzleHttp\Psr7\Response)) #1 C:\www\bomb-shelter\http\vendor\guzzlehttp\promises\src\Promise.php(203): GuzzleHttp\Middleware::GuzzleHttp{closure}(Object(GuzzleHttp\Psr7\Response))

2 C:\www\bomb-shelter\http\vendor\guzzlehttp\promises\src\Promise.php(156):

GuzzleHttp\Promise\Promise::callHandler(1, Object(GuzzleHttp\Psr7\Response), Array) #3 C:\www\bomb-shelter\http\vendor\guzzlehttp\promises\src\TaskQueue.php(47): GuzzleHttp\Promise\Promi in C:\www\bomb-shelter\http\vendor\guzzlehttp\guzzle\src\Exception\RequestException.php on line 113

Any idea how can I make it works on guzzle? or other PHP HTTP client?

1
Please help. thank youAljay
I'm stuck at the same issue , how did you resolve it ?Kamal Saleh

1 Answers

0
votes

What is the truncated portion of the 422 error message you're getting? It might be that you're missing a required variant ID. Using guzzle, here's the request that worked for me, adapted from https://stackoverflow.com/a/39525059/8521556:

<?php
require 'vendor/autoload.php';

$cart = array(
    'customer_id' => 1,
    'line_items' => array(
        array('quantity' => 1, 'product_id' => 1116, 'variant_id' => 1530)
    ),
);

json_encode($cart);

$client = new GuzzleHttp\Client([
    'headers' => [ 
        'Accept' => 'application/json',
        'Content-type' => 'application/json',
        'X-Auth-Client' => 'xxxxxxxxxxxx',
        'X-Auth-Token' => 'xxxxxxxxxxxx' ]
]);

$response = $client->post('https://api.bigcommerce.com/stores/xxxxxxxx/v3/carts',
    ['json' => $cart]
);

echo '<pre>' . var_export($response->getStatusCode(), true) . '</pre>';
echo '<pre>' . var_export($response->getBody()->getContents(), true) . '</pre>';