• Resolved ckmcgowan

    (@ckmcgowan)


    Hi Sebastien,

    With a clean install of Co Cart and a test product added to woo commerce I have tried to use your documented example to simply add a product.

    The response I get is:
    {“code”:”rest_invalid_json”,”message”:”Invalid JSON body passed.”,”data”:{“status”:400,”json_error_code”:4,”json_error_message”:”Syntax error”}}

    Any ideas?
    It appears the get requests are working correctly but there is a problem with POST, I have tried to $json_encode the $args array to test but I then get response of Product Id must be specified??

    Code being called below (as per your documentation).

    Many Thanks

    Chris

    <?php
    $curl = curl_init();

    $args = array(
    ‘product_id’ => “203”,
    ‘quantity’ => 1
    );

    curl_setopt_array( $curl, array(
    CURLOPT_URL => “https://mydomain.com/wp-json/cocart/v1/add-item&#8221;,
    CURLOPT_CUSTOMREQUEST => “POST”,
    CURLOPT_POSTFIELDS => $args,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_TIMEOUT => 30,
    CURLOPT_HTTPHEADER => array(
    ‘Accept: application/json’,
    ‘Content-Type: application/json’,
    ‘User-Agent: CoCart API/v1’,
    )
    ) );

    $response = curl_exec($curl);

    curl_close($curl);

    echo $response;

Viewing 8 replies - 1 through 8 (of 8 total)
  • Plugin Author Sébastien Dumont

    (@sebd86)

    Not sure why but try removing 'Content-Type: application/json' from the CURLOPT_HTTPHEADER array. Does not seem to like it but I guess the REST API is already got the content type set so it’s not needed.

    Hope that helps.

    Thread Starter ckmcgowan

    (@ckmcgowan)

    Thanks – I sorted it by adding CURLOPT_POST => true to the array and it appears to work.
    I get the following back: {“key”:”e2c0be24560d78c5e599c2a9c9d0bbd2″,”product_id”:203,”variation_id”:0,”variation”:[],”quantity”:1,”data”:{},”data_hash”:”b5c1d5ca8bae6d4896cf1807cdf763f0″,”line_tax_data”:{“subtotal”:[],”total”:[]},”line_subtotal”:10,”line_subtotal_tax”:0,”line_total”:10,”line_tax”:0,”product_name”:”Test”,”product_title”:”Test”,”product_price”:”\u00a310.00″}

    Am I correct in thinking if I were to now call the following endpoint via GET
    https://mydomain.com/wp-json/cocart/v1/get-cart?cart_key=e2c0be24560d78c5e599c2a9c9d0bbd2 I should retrieve the cart with the test item I have just added as this does not seem to work but I can’t see any cookies set either?

    What I am attempting to do is redirect away from the woocommerce site to external site that enables a product to be customised and then added to cart via your CoCart and then return the user to woocommerce for them to checkout normally.

    Thanks

    Chris

    Plugin Author Sébastien Dumont

    (@sebd86)

    It’s not working for you because you have set the item key as the cart key.

    See cart key section for more information in the documentation.

    Thread Starter ckmcgowan

    (@ckmcgowan)

    Doh! – Just realised that ??
    Last question – I can’t seem to find away to access the cookie under Set-Cookie you mention for guest cart via a php curl call? if I enable CURL_OPTHEADER I can see the following so can grab the X-CoCart-API value:

    HTTP/1.1 200 OK
    Date: Thu, 07 Jan 2021 10:17:01 GMT
    Server: Apache/2.4.6 (CentOS) OpenSSL/1.0.2k-fips
    X-Powered-By: PHP/7.3.23
    X-Robots-Tag: noindex
    Link: <https://mydomain.com/wp-json/&gt;; rel=”https://api.w.org/&#8221;
    X-Content-Type-Options: nosniff
    Access-Control-Expose-Headers: X-WP-Total, X-WP-TotalPages, Link
    Access-Control-Allow-Headers: Authorization, X-WP-Nonce, Content-Disposition, Content-MD5, Content-Type
    X-CoCart-API: 7747bda0ce7c26307b8f5a0e1dfcd00a
    Allow: GET
    Vary: Origin
    Transfer-Encoding: chunked
    Content-Type: application/json; charset=UTF-8

    But can’t see anything via Set-Cookie? Am I misinterpreting what the API should return on a call?

    Many Thanks

    Chris

    Plugin Author Sébastien Dumont

    (@sebd86)

    So now that you got the headers returned you can use the value from X-CoCart-AP to set your cart_key.

    Thread Starter ckmcgowan

    (@ckmcgowan)

    Yes I realise this but I was just wondering the best way to beable to retrieve that cart key on the woo commerce site after I direct the customer back so what I have added via the CartCo API then shows in the cart on the woocomerce site so the customer can complete the checkout as normal.

    Plugin Author Sébastien Dumont

    (@sebd86)

    Ah. If you are wanting to then load the cart via the web you will need to load the cart from session

    Thread Starter ckmcgowan

    (@ckmcgowan)

    Perfect – exactly what I was looking for ??

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘PHP Post Returns Invalid JSON on clean install’ is closed to new replies.