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?