10

I have tried calling [POST] /carts/mine/items, headers with correct bearer, and body:

{
    "cart_item": 1,
    "sku": "MY_SKU",
    "qty": 1
}

and I get the folowing response:

{
   "message": "Invalid value of \"%value\" provided for the %fieldName field.",
   "parameters": {
      "fieldName": "qty",
      "value": null
   }
}

Two things, I do not understand what to put in cart_item (but it is required) and I do not why it keeps telling me qty is null?

awavi
  • 837
  • 3
  • 11
  • 23

2 Answers2

12

First of all empty cart should be created using request with empty body:

[POST] {base URL}/rest/V1/carts/mine

In response you will get ID of your quote.

Now you can add items to your cart using:

[POST] {base URL}/rest/V1/carts/mine/items
{
  "cart_item": {
    "quote_id": <cart ID received from previous call>,
    "sku": "product_sku",
    "qty": 10
  }
}

In response you should get your cart item data:

{
  "item_id": 1,
  "sku": "product_sku",
  "qty": 10,
  "name": "Simple Product",
  "price": 123,
  "product_type": "simple",
  "quote_id": "1"
}

Be careful since you may accidentally update existing cart item quantity with POST request, if execute the same request several times.

SagarPPanchal
  • 9,839
  • 6
  • 34
  • 62
Alex Paliarush
  • 337
  • 4
  • 6
0

It is an addition to @Alex Palirush's answer thanks to explain it clearly.

The quote id must be integer otherwise it will through an error unknown field cartId.

{
  "message": "No such entity with %fieldName = %fieldValue",
  "parameters": {
    "fieldName": "cartId",
    "fieldValue": "0"
  }
}
ponury-kostek
  • 7,824
  • 4
  • 23
  • 31
MOHD TAHIR
  • 151
  • 1
  • 1
  • 11
  • {"sku": "T-shirt","qty": 2, "quote_id": 37, "product_type": "configurable","product_option": {"extension_attributes": {"configurable_item_options": [{"option_id": "167", "option_value": "7"}, {"option_id": "93", "option_value": "5"}]}}} Here are my parameters and check the above comments as well but @MOHD TAHIR still gives an error for the cartId no such entity with the field name. Do you have any solution for this. Appreciate in advance – Kartik Asodariya Mar 24 '20 at 11:37
  • having same problem, guest works fine, logged-in doesnt, returns this error message: "\"%fieldName\" is required. Enter and try again." parameters: {fieldName: 'quoteId'} – Adrian Jan 05 '22 at 18:31