Salesforce Commerce Cloud - Add search subrequest in batch OCAPI

1.5k views Asked by At

I'm tying to write a batch OCAPI.

In one of the sub request, I would like to use the product_search and variant_search OCAPI.

All settings in the sandbox are correct, the two OCAPI works perfectly if I use them separately.

I receive an error only if I'm using them in a batch.

Here is my request:

POST /s/-/dw/batch?client_id=xxx HTTP/1.1
Host: xxx-alliance-prtnr-eu09-dw.demandware.net
Content-Type: multipart/mixed; boundary=23dh3f9f4
Authorization: Bearer xxx

--23dh3f9f4
x-dw-content-id: req4
x-dw-http-method: POST
x-dw-resource-path-extension: /s/-/dw/data/v18_8/product_search

{
    "query" : {
        "text_query": {
            "fields": ["id"],
            "search_phrase": "73910432"
        }
    },
    "select" : "(hits.(product_id))"
}

--23dh3f9f4
x-dw-http-method: POST
x-dw-content-id: req3
x-dw-resource-path: /s/-/dw/data/v18_8/products/
x-dw-resource-path-extension: 73910432/variant_search

{
  "query": {
    "text_query": {
      "fields": [
        "variation_attribute.size"
      ],
      "search_phrase": "34"
    }
  },
  "select": "(hits.(product_id,variation_values))"
}

--23dh3f9f4--

The response from the server:

--23dh3f9f4
x-dw-content-id: req4
Accept-Encoding: gzip, deflate, br
Content-Type: application/json
Content-Length: 95
Cache-Control: no-cache
x-dw-status-code: 500

{"_v":"18.8","fault":{"type":"InternalServerErrorException","message":"Internal Server Error"}}
--23dh3f9f4
x-dw-content-id: req3
Accept-Encoding: gzip, deflate, br
Content-Type: application/json
Content-Length: 215
Cache-Control: no-cache
x-dw-status-code: 400

{"_v":"18.8","fault":{"arguments":{"parameter":"Body"},"type":"NullConstraintViolationException","message":"The null value constraint for parameter 'Body' is violated. Null is not allowed. Please provide a value."}}
--23dh3f9f4--

Any idea?

Thanks for hour help,

1

There are 1 answers

0
sholsinger On

For the 'req4' subrequest

It is likely due to misuse of the 'path' headers. You should have the following in your main (/batch request) header:

x-dw-resource-path: /s/-/dw/data/v18_8/

Then within the req4 headers you'd have:

x-dw-resource-path-extension: product_search

Similarly, in req3 you should have the following header:

x-dw-resource-path-extension: products/73910432/variant_search

Note that you should not need to include x-dw-resource-path in the batched sub-request headers. Only in the main /batch request header. in my example above, you can see that path I've specified at the batch 'request' level is common across all sub-requests. Therefore I only need to specify the sub-request's resource path. That said, if you want to batch storefront & data requests you may need to override the base path to switch between sites or shop resources.

Analyzing your example produces the following two request URIs:

req4

This one may not go anywhere because you've actually not specified any x-dw-resource-path for this request. I can only assume that you end up with something like:

null/s/-/dw/data/v18_8/product_search

req3

/s/-/dw/data/v18_8/products/73910432/variant_search

For the error in req3 (likely both)

This seems like it might be due to malformation of the request. I see that there are two line endings between the last character } and the boundary in both requests. Try reducing that to only one line ending and see if it helps.