Magento 2 – Add Product to Cart with Custom Option Type File via REST API

apicartmagento-2.1magento2rest

I'm trying to add a product to cart using the REST API. I'm using a custom option to add an image, but it doesn't seem to work. This is my json object that I'm sending. The option 7819 which is text is added, but the file isn't. I've tried setting the option_value to the base64 string, but no luck.

'{
  "cartItem": {
    "sku": "f6e957d1-51b2-4a56-90be-dc3c877e3e3b",
    "qty": 1,
    "quote_id": "1234",
    "product_option": {
      "extension_attributes": {
        "custom_options": [
          {
            "option_id": "7818",
            "option_value": "file.jpg",
            "extension_attributes": {
              "file_info": {
                "base64_encoded_data": "/9j/4AAQSkZJRgABAQAAAQABAAD//gA7Q1JFQVRPUj ...",
                "type": "image/jpeg",
                "name": "file.jpg"
              }
            }
          },
          {
          "option_id": "7819",
          "option_value": "test"
          }
        ]
      }
    }
  }
}'

Greetings

Best Answer

The option_value should be "file":

'{
  "cartItem": {
    "sku": "f6e957d1-51b2-4a56-90be-dc3c877e3e3b",
    "qty": 1,
    "quote_id": "1234",
    "product_option": {
      "extension_attributes": {
        "custom_options": [
          {
            "option_id": "7818",
            "option_value": "file",
            "extension_attributes": {
              "file_info": {
                "base64_encoded_data": "/9j/4AAQSkZJRgABAQAAAQABAAD//gA7Q1JFQVRPUj ...",
                "type": "image/jpeg",
                "name": "file.jpg"
              }
            }
          },
          {
          "option_id": "7819",
          "option_value": "test"
          }
        ]
      }
    }
  }
}'