sharepoint-onlinesharepoint-rest-apibatch-request

SharePoint REST API Online batch request issues : getting 400 Bad Request, odata.error Invalid request


I'm having a bit of trouble isolating a batch request using the _api/$batch request. I'm sure there is just some simple mistake/issue going on, but I can't just pinpoint this. Any ideas?

Here are some snippets of the raw postman console logs for the request:

POST /_api/$batch HTTP/1.1
Accept: application/json; odata=nometadata
Content-Type: multipart/mixed; boundary="batch_8215457c-c4fb-432a-abf6-7b1f86168c65"
Authorization: Bearer <token deleted>

--batch_8215457c-c4fb-432a-abf6-7b1f86168c65
Content-Type: multipart/mixed; boundary="changeset_d9097ba9-2d69-40ca-a301-90cf97aafb17"
Content-Transfer-Encoding: binary

--changeset_d9097ba9-2d69-40ca-a301-90cf97aafb17
Content-Type: application/http
Content-Transfer-Encoding: binary

DELETE https://myOrg.sharepoint.com/sites/mySite/_api/web/lists/getByTitle(myList)/items(27) HTTP/1.1
If-Match: *
Accept: application/json; odata=nometadata

--changeset_d9097ba9-2d69-40ca-a301-90cf97aafb17
Content-Type: application/http
Content-Transfer-Encoding: binary

DELETE https://myOrg.sharepoint.com/sites/mySite/_api/web/lists/getByTitle(myList)/items(30) HTTP/1.1
If-Match: *
Accept: application/json; odata=nometadata

--changeset_d9097ba9-2d69-40ca-a301-90cf97aafb17--

--batch_8215457c-c4fb-432a-abf6-7b1f86168c65--

The response I get back for each item to be deleted is 400 Bad Request with each Batch response also containing: {"odata.error":{"code":"-1, Microsoft.SharePoint.Client.InvalidClientQueryException","message":{"lang":"en-US","value":"Invalid request."}}}

--batchresponse_38add954-a201-4857-a6db-68067eb9bc0a
Content-Type: application/http
Content-Transfer-Encoding: binary

HTTP/1.1 400 Bad Request
CONTENT-TYPE: application/json;odata=nometadata;streaming=true;charset=utf-8

{"odata.error":{"code":"-1, Microsoft.SharePoint.Client.InvalidClientQueryException","message":{"lang":"en-US","value":"Invalid request."}}}
--batchresponse_38add954-a201-4857-a6db-68067eb9bc0a
Content-Type: application/http
Content-Transfer-Encoding: binary

HTTP/1.1 400 Bad Request
CONTENT-TYPE: application/json;odata=nometadata;streaming=true;charset=utf-8

{"odata.error":{"code":"-1, Microsoft.SharePoint.Client.InvalidClientQueryException","message":{"lang":"en-US","value":"Invalid request."}}}
--batchresponse_38add954-a201-4857-a6db-68067eb9bc0a--

I've been digging and have tried a few things:

Can a helpful eye point out the mistake?


Solution

  • This was a bad interpretation of the documentation on my part.

    According to Microsoft's documentation, the section on the Request URL states (emphasis mine):

    The request URL consists of the root service URL and the $batch option; for example, https://fabrikam.sharepoint.com/_api/$batch or https://fabrikam.office365.com/api/v1.0/me/$batch.

    However, the "root service URL" is a bit misleading, as it's actually supposed to be the site's URL. I would have considered the root service URL:

    https://myorg.sharepoint.com
    

    So, in my example above, it's:

    POST <siteURL>/_api/$batch  
    

    Or, more specific to the example:

    POST https://yourorg.sharepoint.com/sites/mySite  HTTP/1.1
    

    Thanks for everyone who put some thought into this.