I have Batch route enabled in config, please refer below code.
namespace WebAPI {
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
// Web API routes
config.MapHttpAttributeRoutes();
config.Services.Replace(typeof(IExceptionHandler), new CustomExceptionHandler());
config.MapODataServiceRoute(routeName: "OData",
routePrefix: "",
model: APIConfiguration.GetModel(),
batchHandler: new DefaultODataBatchHandler(GlobalConfiguration.DefaultServer));
}
} }
I am using below code to use http client to post a batch.
Dim batchContent As New MultipartContent("mixed", "--testDataBoundary---")
'Parent
Dim RqP As New HttpRequestMessage(HttpMethod.Put, String.Format("{0}Projects({1})/", APIURI, projectRequest.project.ProjectID))
RqP.Content = New StringContent(Newtonsoft.Json.JsonConvert.SerializeObject(projectRequest.project, microsoftDateFormatSettings))
Dim ProjectContent As New HttpMessageContent(RqP)
If ProjectContent.Headers.Contains("Content-Type") Then ProjectContent.Headers.Remove("Content-Type")
ProjectContent.Headers.Add("Content-Type", "multipart/mixed; boundary=--testPTDataBoundary---")
'ProjectContent.Headers.Add("Content-Type", "application/http")
ProjectContent.Headers.Add("Content-Transfer-Encoding", "binary")
If ProjectContent.Headers.Contains("contentTypeMime-Part") Then ProjectContent.Headers.Remove("contentTypeMime-Part")
'ProjectContent.Headers.Add("contentTypeMime-Part", "Content-Type:application/http;")
ProjectContent.Headers.Add("contentTypeMime-Part", "Content-Type:multipart/mixed;")
batchContent.Add(ProjectContent)
'Child1
Dim report As Report = projectRequest.project.Reports.Take(1).First()
Dim RqR As New HttpRequestMessage(HttpMethod.Put, String.Format("{0}Reports({1})/", APIURI, report.ReportID))
RqR.Content = New StringContent(Newtonsoft.Json.JsonConvert.SerializeObject(report, microsoftDateFormatSettings))
Dim ReportContent As New HttpMessageContent(RqR)
If ReportContent.Headers.Contains("Content-Type") Then ReportContent.Headers.Remove("Content-Type")
ReportContent.Headers.Add("Content-Type", "multipart/mixed; boundary=--testPTDataBoundary---")
'ReportContent.Headers.Add("Content-Type", "application/http")
ReportContent.Headers.Add("Content-Transfer-Encoding", "binary")
If ReportContent.Headers.Contains("contentTypeMime-Part") Then ReportContent.Headers.Remove("contentTypeMime-Part")
'ReportContent.Headers.Add("contentTypeMime-Part", "Content-Type:application/http;")
ReportContent.Headers.Add("contentTypeMime-Part", "Content-Type:multipart/mixed;")
batchContent.Add(ReportContent)
Dim batchRequest As HttpRequestMessage = New HttpRequestMessage(HttpMethod.Post, APIURI& "/$batch/")
batchRequest.Content = batchContent
Dim APIResponseBatch = Await Client.SendAsync(batchRequest)
Dim streamProvider = APIResponseBatch.Content.ReadAsMultipartAsync().Result()
For Each cnt As HttpContent In streamProvider.Contents
lblErrorMsg.Text &= cnt.ReadAsStringAsync().Result
Next
Initially I was getting following exception and then I added contentTypeMime-Part to each request of the batch.
A missing or invalid 'Content-Transfer-Encoding' header was found. The 'Content-Transfer-Encoding' header must be specified for each batch operation, and its value must be 'binary'
I am able to club multiple GETs in one Batch and data is being fetched for all requests. But for post (Parent and Children in my case) I am getting following exeption;
Nested change sets in a batch payload are not supported.
Is this limitation of oData using asp.net web API2 or I am missing something here?