By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement . We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Before v107 I was able to add a string body with a content type of multipart/mixed; boundary=changeset as follows:
new RestRequest("resource") { Body = new RequestBody("multipart/mixed; boundary=changeset", string.Empty, "body") };

I have updated this to
new RestRequest("resource").AddBody("body", "multipart/mixed; boundary=changeset");

But this results in a format exception within System.Net.Http code:
System.FormatException: The format of value 'multipart/mixed; boundary=changeset' is invalid.

I have also tried AddStringBody , and using the FormBoundary property but was not able to get around this exception.

I found this related issue: dotnet/runtime#20272 however I was not able to work out how to use any of their workarounds.

To Reproduce

var client = new RestClient("baseUrl");
var request = new RestRequest("resource").AddBody("body", "multipart/mixed; boundary=changeset");
await client.PostAsync(request);

Expected behavior
To be able to send a string body, with the content type "multipart/mixed; boundary=changeset".

Stack trace

System.FormatException: The format of value 'multipart/mixed; boundary=changeset' is invalid.
   at System.Net.Http.Headers.MediaTypeHeaderValue.CheckMediaTypeFormat(String mediaType, String parameterName)
   at System.Net.Http.Headers.MediaTypeHeaderValue..ctor(String mediaType)
   at System.Net.Http.StringContent..ctor(String content, Encoding encoding, String mediaType)
   at RestSharp.RequestContent.Serialize(BodyParameter body)
   at RestSharp.RequestContent.AddBody(Boolean hasPostParameters)
   at RestSharp.RequestContent.BuildContent()
   at RestSharp.RestClient.ExecuteInternal(RestRequest request, CancellationToken cancellationToken)
   at RestSharp.RestClient.ExecuteAsync(RestRequest request, CancellationToken cancellationToken)
   at RestSharp.RestClientExtensions.PostAsync(RestClient client, RestRequest request, CancellationToken cancellationToken)
          

You can use

var request = new RestRequest("resource") { FormBoundary = "changeset" }
     .AddHeader(KnownHeaders.ContentType, "multipart/mixed");

When RestSharp sees you are setting the content type header without the actual body, it will add an empty body.

You can use

var request = new RestRequest("resource") { FormBoundary = "changeset" }
     .AddHeader(KnownHeaders.ContentType, "multipart/mixed");

When RestSharp sees you are setting the content type header without the actual body, it will add an empty body.

I tried this, along with AddStringBody("body", DataFormat.None) after the AddHeader, but the Content-Type was only set to multipart/mixed and not multipart/mixed; boundary=changeset

It would help if you specify how do you need your request to look like.

I am trying to use a batch request of an OData API, similar to the example here https://www.odata.org/documentation/odata-version-3-0/batch-processing/

The body is a string I have already built.
The Content-Type header needs to be multipart/mixed; boundary=changeset.

I got this code:

var request = new RestRequest("resource") { FormBoundary = "changeset", AlwaysMultipartFormData = true};
request.AddStringBody(json, ContentType.Json);
var response = await client.PostAsync(request);

and it produces the following result:

That works in regards to setting the Content-Type header correctly, thanks.

The next problem is, I need to format the request exactly like this:

Header:
Content-Type: multipart/mixed; boundary=changeset

Body:

--batch
Content-Type: multipart/mixed; boundary=changeset
--changeset
Content-Type: application/http 
Content-Transfer-Encoding: binary 
Content-ID: 1
PUT https://api.com HTTP/1.1
Content-Type: application/json
{ "foo": "bar1" }
--changeset
Content-Type: application/http 
Content-Transfer-Encoding: binary 
Content-ID: 2
PUT https://api.com HTTP/1.1
Content-Type: application/json
{ "foo": "bar2" }
--changeset
--batch--

The set of --changeset's are wrapped with a --batch / --batch--.
The same Content-Type as the header is repeated after the opening --batch.
Previously I created the entire body as a string, from the opening--batch to the closing --batch-- with all of the --changeset's in between.
Using the method above I have tried AddStringBody() and AddBody() but it always wraps my body within a --changeset.

Is this supported?