1

My objective is to process emails in a folder and then move them to another folder to which I have the id. To ease the workload I'm trying to make use of the batch functionality.

Unfortunately, every time I try to run the batch function I'm presented with an exception with the message Code: invalidRequest Message: Unable to deserialize content..

The code in question, simplified for just one request, can be found below.

var batch = new BatchRequestContent();

var json = JsonSerializer.Serialize( new { destinationId =  Graph.Me.Messages[messageId].Move(folderId).Request().RequestBody.DestinationId } );
var jsonMessage = Graph.HttpProvider.Serializer.SerializeAsJsonContent(json);

var request = Graph.Me.Messages[messageId].Move(folderId).Request().GetHttpRequestMessage();
request.Method = HttpMethod.Post;
request.Content = jsonMessage;

batch.AddBatchRequestStep(request);

var res = await Graph.Batch.Request().PostAsync(batch);

I've narrowed down the problem to be about the request.Content because without that it will go through, though getting back with a 400 error about missing body.

Copying the string from batch.ReadAsStringAsync() and pasting that directly into the Graph Explorer and using that to run the query returns a 200 success.

Based on what I've tried I'm starting to lean on it being a limitation of the SDK's batch. Any ideas?

Sekuiya
  • 11
  • 2

1 Answers1

0

While this wasn't the solution I was looking for, it works as a band aid solution.

Basically, after you add all your steps to the BatchRequestContent you use the ReadAsStringAsync() to get the body of the request. Then you use the library itself to send the HTTP Request, as instructed here.

HttpRequestMessage hrm = Graph.Batch.Request().GetHttpRequestMessage();
hrm.Method = HttpMethod.Post;
hrm.Content = Graph.HttpProvider.Serializer.SerializeAsJsonContent(await batch.ReadAsStringAsync());

// Authenticate (add access token) our HttpRequestMessage
await Graph.AuthenticationProvider.AuthenticateRequestAsync(hrm);

// Send the request and get the response.
HttpResponseMessage res = await Graph.HttpProvider.SendAsync(hrm);
Sekuiya
  • 11
  • 2