Force body Content-Type to another value for file upload

2,975 views
Skip to first unread message

Mark

unread,
Sep 26, 2011, 1:39:43 PM9/26/11
to RestSharp
Hi,

Is there a way to set the Content-Type when using AddFile for a file
upload?

I recall seeing somewhere that RestSharp will change it to multipart
whenever you use AddFile.

The service I'm hitting throws the following error:

<Error>
<Message>Content-Type "multipart/form-data;
boundary=-----------------------------28947758029299" is unsupported</
Message>
<Server-Time>2011-09-26T13:28:29</Server-Time>
<Id>2D643063-EE74-4D33-9AA3-E86230F8D5D8</Id>
</Error>


Here's what the doc says for the service I'm hitting:
The body of all POST requests must be XML 1.0 using UTF-8 encoding:

Content-Type: application/xml


Any help is really appreciated.

Thanks!

Andrew Young

unread,
Sep 26, 2011, 1:49:59 PM9/26/11
to rest...@googlegroups.com
You can change the ContentType of the file itself by using the appropriate overload:

public RestRequest AddFile(string name, byte[] bytes, string fileName, string contentType)

But you cannot change the ContentType of the request as a whole.

Andrew Young

unread,
Sep 26, 2011, 2:03:26 PM9/26/11
to rest...@googlegroups.com
Sorry for the misinformation. You can also use

public RestRequest AddHeader(string name, string value)

to change content type.

restRequest.AddHeader("Content-Type", "application/xml");

Mark

unread,
Sep 26, 2011, 2:23:43 PM9/26/11
to RestSharp
Thanks for the reply Andrew.

I'm already setting the AddFile contentType and also tried adding it
as a header, but still get the same response:

Content-Type "multipart/form-data;
boundary=-----------------------------28947758029299" is unsupported

Any other suggestions?

Andrew Young

unread,
Sep 26, 2011, 3:48:54 PM9/26/11
to rest...@googlegroups.com
If the only thing you're sending is an xml file (i.e. not multipart). You could read the file as a string with System.IO and set the content of the body using 

restRequest.AddParameter("application/xml", xmlString, ParameterType.RequestBody);

The comments do not recommend using AddParameter with RequestBody directly in this way but I see no other way to directly set the content of the body. Your case might be an exception.

I don't see a way to directly add binary content to the body though.

Maybe John Sheehan can comment on the reasoning behind this design.

Mark

unread,
Sep 26, 2011, 7:04:21 PM9/26/11
to RestSharp
Thanks, I'll play around with it a bit more. I'm actually posting a
PDF document, but I can't tell how it's handled in the background.

It's just frustrating as I was able to perform this operation in REST
Console (a very cool Chrome extension, IMHO), but I need to build an
app to do it.



On Sep 26, 3:48 pm, Andrew Young <andrewdyo...@gmail.com> wrote:
> If the only thing you're sending is an xml file (i.e. not multipart). You could read the file as a string with System.IO and set the content of the body using
>
> restRequest.AddParameter("application/xml", xmlString, ParameterType.RequestBody);
>
> The comments do not recommend using AddParameter with RequestBody directly in this way but I see no other way to directly set the content of the body. Your case might be an exception.
>
> I don't see a way to directly add binary content to the body though.
>
> Maybe John Sheehan can comment on the reasoning behind this design.
>
>
>
>
>
>
>
> On Monday, September 26, 2011 at 11:23, Mark wrote:
> > Thanks for the reply Andrew.
>
> > I'm already setting the AddFile contentType and also tried adding it
> > as a header, but still get the same response:
>
> > Content-Type "multipart/form-data;
> > boundary=-----------------------------28947758029299" is unsupported
>
> > Any other suggestions?
>

Mark

unread,
Sep 27, 2011, 12:45:59 PM9/27/11
to RestSharp
My mistake in the title. I actually want to force the HEADER Content-
Type parameter to another value when using a file upload.

I can now see that the ContentType gets forced to "multipart/form-
data" if there are files to be uploaded.  Can anybody suggest a way
that I can force the header Content-Type parameter to another value
without actually changing and rebuilding the library? Or does it make
more sense to rebuild the library?

I've isolated it in the RestSharp library under Http.cs:
private void PreparePostBody(HttpWebRequest webRequest) {
if(HasFiles) { webRequest.ContentType =
GetMultipartFormContentType(); } else if(HasParameters) {
webRequest.ContentType = "application/x-www-form-urlencoded";
RequestBody = EncodeParameters(); } else if(HasBody) {
webRequest.ContentType = RequestContentType; } }


Thanks!

Andrew Young

unread,
Sep 27, 2011, 12:50:44 PM9/27/11
to rest...@googlegroups.com
You will have to rebuild RestSharp. It would be nice to have PreparePostBody to detect whether it is actually a multipart request vs. just assuming that it is a multipart if there are files attached.

If you end up fixing it, send a pull request and we'll consider adding it to RestSharp.

Mark

unread,
Sep 27, 2011, 1:36:06 PM9/27/11
to RestSharp
Thanks for your help, Andrew.

I'm really just a hack at this (very new to C#), I'm actually using
this from VB. I'll give it a shot and see if I can figure out
something useful.

My guess is that I would need to count the number of files to
determine if this is multipart or not and then act accordingly.



On Sep 27, 12:50 pm, Andrew Young <andrewdyo...@gmail.com> wrote:
> You will have to rebuild RestSharp. It would be nice to have PreparePostBody to detect whether it is actually a multipart request vs. just assuming that it is a multipart if there are files attached.
>
> If you end up fixing it, send a pull request and we'll consider adding it to RestSharp.
>
>
>
>
>
>
>
> On Tuesday, September 27, 2011 at 9:45, Mark wrote:
> > My mistake in the title. I actually want to force the HEADER Content-
> > Type parameter to another value when using a file upload.
>
> > I can now see that the ContentType gets forced to "multipart/form-
> > data" if there are files to be uploaded. Can anybody suggest a way
> > that I can force the header Content-Type parameter to another value
> > without actually changing and rebuilding the library? Or does it make
> > more sense to rebuild the library?
>
> > I've isolated it in the RestSharp library under Http.cs:
> >  private void PreparePostBody(HttpWebRequest webRequest) {
> > if(HasFiles) { webRequest.ContentType =
> > GetMultipartFormContentType(); } else if(HasParameters) {
> > webRequest.ContentType = "application/x-www-form-urlencoded";
> > RequestBody = EncodeParameters(); } else if(HasBody) {
> > webRequest.ContentType = RequestContentType; } }
>
> > Thanks!
>
> > On Sep 26, 7:04 pm, Mark <markpop...@gmail.com (http://gmail.com)> wrote:
> > > Thanks, I'll play around with it a bit more. I'm actually posting a
> > > PDF document, but I can't tell how it's handled in the background.
>
> > > It's just frustrating as I was able to perform this operation in REST
> > > Console (a very cool Chrome extension, IMHO), but I need to build an
> > > app to do it.
>

Andrew Young

unread,
Sep 27, 2011, 1:49:07 PM9/27/11
to rest...@googlegroups.com
Not only that but it would need to detect whether there's any body content and/or form values to post.
Message has been deleted

dresel

unread,
Aug 9, 2012, 11:04:11 AM8/9/12
to rest...@googlegroups.com
See http://stackoverflow.com/a/11886210/1249506.

Am Donnerstag, 9. August 2012 17:03:26 UTC+2 schrieb dresel:
Reply all
Reply to author
Forward
0 new messages