1

I am facing one issue. I am adding configurable product to cart with its image,size and color but getting the below error using magento-2 REST API.

Error:

{"message":"The image content must be valid base64 encoded data."}

I am adding my code below.

$user_id=$_POST['user_id'];
$psku=$_POST['sku'];
$qty=$_POST['quantity'];
$isConfigurable=$_POST['isConfigurable'];
$image=$_POST['image_url'];
$image_id=$_POST['image_id'];
$escaped_url=str_replace( '\/', '/', $image);
$type = pathinfo($escaped_url, PATHINFO_EXTENSION);
$data = file_get_contents($escaped_url);
$base64=base64_encode($escaped_url);
$itype='image/'.$type;
//$itype=str_replace( '\/', '/',$itype);
//echo $itype;exit;
$iname=basename($escaped_url);
//echo $base64;exit;
if ($isConfigurable==1) {
    if (isset($_POST['size_attribute_value'])) {
        $size_attribute_value=$_POST['size_attribute_value'];
    }else{
        $size_attribute_value='';
    }
    if (isset($_POST['size_attribute_id'])) {
        $size_attribute_id=$_POST['size_attribute_id'];
    }else{
        $size_attribute_id='';
    }
    if (isset($_POST['color_attribute_value'])) {
        $color_attribute_value=$_POST['color_attribute_value'];
    }else{
        $color_attribute_value='';
    }
    if (isset($_POST['color_attribute_id'])) {
        $color_attribute_id=$_POST['color_attribute_id'];
    }else{
        $color_attribute_id='';
    }
}
if ($isConfigurable==1) {
            $ettr=array();
            $citems=array(array("option_id"=>$size_attribute_id,"option_value"=>$size_attribute_value),array("option_id"=>$color_attribute_id,"option_value"=>$color_attribute_value),array("option_id"=>$image_id,"option_value"=>"file","extension_attributes"=>array("file_info"=>array("base64_encoded_data"=>$base64,"type"=>$itype,"name"=>$iname))));
            //$poption=array("extension_attributes"=>array("configurable_item_options"=>$citems));
            $poption=array("extension_attributes"=>array("custom_options"=>$citems));
            $item=array("quote_id"=>$quote,"sku"=>$psku,"qty"=>$qty,"product_option"=>$poption);
            $cartData=array("cartItem"=>$item);
        }else{
            $citems=array(array("option_id"=>$image_id,"option_value"=>"file","extension_attributes"=>array("file_info"=>array("base64_encoded_data"=>$base64,"type"=>$itype,"name"=>$iname))));
            $poption=array("extension_attributes"=>array("custom_options"=>$citems));
            $item=array("quote_id"=>$quote,"sku"=>$psku,"qty"=>$qty,"product_option"=>$poption);
            $cartData=array("cartItem"=>$item);
        }
        //echo json_encode($cartData);exit;
        $requestUrl ='http://example.com/index.php/rest/V1/carts/mine/items';
        $ch = curl_init($requestUrl);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($cartData));
        curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Authorization: Bearer " . json_decode($token1)));
        $result = json_decode(curl_exec($ch));
        //print_r($result);exit;
        $array = json_decode(json_encode($result), True);
        echo json_encode($array);exit;

My json format boy input are like below.

{"cartItem":{"quote_id":"202","sku":"KB067","qty":"1","product_option":{"extension_attributes":{"custom_options":[{"option_id":"225","option_value":"175"},{"option_id":"93","option_value":"163"},{"option_id":"147","option_value":"file","extension_attributes":{"file_info":{"base64_encoded_data":"aHR0cDovL21hZ2VudG8tMTQ4NDg2LTQyNTc3NS5jbG91ZHdheXNhcHBzLmNvbS9wdWIvbWVkaWEvY2F0YWxvZy9wcm9kdWN0Ly8xLzEvMTEuanBlZw==","type":"image\/jpeg","name":"11.jpeg"}}}]}}}}

Here I am giving the full path of image(i.e-http:\/\/example.com\/pub\/media\/catalog\/product\/\/1\/1\/11.jpeg) and its id as input. Please help me to resolve this error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.