microsoft-graph-apibatch-request

Batch request returning 400 bad request if the file name contains '#' while trying to upload a file to onedrive


I have a file named 'File#' which I am trying to createUploadSession to upload it to onedrive.

When I try this API

Url :

https://graph.microsoft.com/v1.0/me/drive/items/{itemId}/File#.txt:/createUploadSession

Body :

{
  "item": {
    "@microsoft.graph.conflictBehavior": "replace"
  }
}

I get a Success 200 response, but when I try to run the same url with a batch request I get a 400 Bad request even when both the requests are same.

P.S: I have noticed it only happens with the file names which have # in them

I was expecting the batch request to behave in a same way as single independent request.


Solution

  • Try to encode # character as %23:

    POST https://graph.microsoft.com/v1.0/$batch
    {
        "requests": [
            {
                "url": "/me/drive/items/{item_id}:/File%23.txt:/createUploadSession",
                "method": "POST",
                "id": "1",
                "body": {
                    "item": {
                        "@microsoft.graph.conflictBehavior": "replace"
                    }
                },
                "headers": {
                    "Content-Type": "application/json"
                }
            }
        ]
    }