PUT's dont honor the RequestFormat

115 views
Skip to first unread message

Nick

unread,
Oct 27, 2011, 4:48:25 PM10/27/11
to RestSharp
...or put another way, there is no obvious way to specify the content-
type that I am sending.

If I try to put like so:

RestClient client = new RestClient(someUrl);
RestRequest req = new RestRequest(someRoute);
req.Method = RestSharp.Method.PUT;
req.RequestFormat = DataFormat.Json;
req.AddBody(someobject);
RestResponse res = client.Execute(req);

the someobject object will NOT be posted as JSON - it will be posted
as Content-Type: application/x-www-form-urlencoded (checked with
fiddler and on the server side)

Either I am doing something wrong or this is a pretty huge bug.

Al West

unread,
Oct 30, 2011, 5:53:35 PM10/30/11
to rest...@googlegroups.com
Do you mean to use req.RequestFormat = Method.POST ; ?

nick caramello

unread,
Oct 30, 2011, 6:15:23 PM10/30/11
to rest...@googlegroups.com

I do not.  I mean to PUT (update) data.

philwinkel

unread,
Dec 28, 2011, 6:33:50 PM12/28/11
to RestSharp
bump, I'm experiencing this issue as well. I mentioned it in another
thread, but I'll do it again here because something seems a bit off:

request.AddFile("image/jpeg",
(requestStream) =>
{
using (MemoryStream ms = new MemoryStream(jpegBytes))
{
ms.CopyTo(requestStream);
ms.Flush();
ms.Close();
}
},
fileName,
"image/jpeg");
It uploads the JPEG as expected, but the Content Type that I've
specified is not being assigned to the PUT request. It is spewing raw
HTTP headers into my JPEG file, creating an invalid .jpg file:

-------------------------------28947758029299Content-Disposition:
form-
data; name="image/jpeg"; filename="charts/chart.jpg"Content-Type:
application/octet-stream

*RAW JPEG DATA HERE. *

-------------------------------28947758029299--

If I trim the HTTP header text off the top and the bottm of the JPEG
content, the JPEG file is valid. It's this stuff that is getting
spewed into the file that I need to figure out how to fix.

philwinkel

unread,
Dec 28, 2011, 6:44:22 PM12/28/11
to RestSharp
Problem is here.

private void WriteMultipartFormData(Stream requestStream)
{
foreach (var param in Parameters)
{
WriteStringTo(requestStream, GetMultipartFormData(param));
}

foreach (var file in Files)
{
// Add just the first part of this param, since we will write the
file data directly to the Stream
WriteStringTo(requestStream, GetMultipartFileHeader(file));

// Write the file data directly to the Stream, rather than
serializing it to a string.
file.Writer(requestStream);
WriteStringTo(requestStream, _lineBreak);
}

WriteStringTo(requestStream, GetMultipartFooter());
}

Specifically, these two lines:

WriteStringTo(requestStream, GetMultipartFileHeader(file));
WriteStringTo(requestStream, GetMultipartFooter());

I don't know if Dropbox API does something weird or what, but when I
comment out these two lines it stops spewing that text into my JPEG
file contents, and the JPEG file is uploaded as intended.

Andrew Young

unread,
Dec 28, 2011, 6:53:48 PM12/28/11
to rest...@googlegroups.com
This is intended. What RestSharp is doing (90% sure) is that it sends files as multipart requests. So in your previous email what you're seeing is   your file being sent as a multipart request but there's only one part, your file, to the multipart. I don't think RestSharp detects if it needs to send the file as a multipart; it just assumes it does. I think the more correct approach would be to have RestSharp detect whether it needs to send it as a multipart.

philwinkel

unread,
Dec 29, 2011, 7:30:49 PM12/29/11
to RestSharp
Well I'm no HTTP protocol expert, but something seems a bit off if
it's spewing HTTP headers into the file that's being uploaded - lol.

Either that, or dropbox is for some reason spewing it in there... but
I have a feeling it's restsharp

Andrew Young

unread,
Dec 29, 2011, 7:34:50 PM12/29/11
to rest...@googlegroups.com
The format is correct for multipart request bodies. Each part of the multipart should provide its own mime-type. And that is what you're seeing.

Juan J. Chiw

unread,
Feb 29, 2012, 10:02:05 PM2/29/12
to rest...@googlegroups.com
I'm with  philwinkel 

If I want to put a resource (image, music, text) it should be uploaded as the content-type I'm putting in the request.

Because I'm not doing a multipart request, It's just one body with a resource like....

I forked the source and made some changes added a property to RestClient UploadRaw and it just skips the lines commented by philwinkel

the method look like this

private void WriteMultipartFormData(Stream requestStream)
{
if (!UploadRaw)
{
foreach (var param in Parameters)
{
WriteStringTo(requestStream, GetMultipartFormData(param));
}
}
foreach (var file in Files)
{
// Add just the first part of this param, since we will write the file data directly to the Stream
if (!UploadRaw)
WriteStringTo(requestStream, GetMultipartFileHeader(file));

// Write the file data directly to the Stream, rather than serializing it to a string.
file.Writer(requestStream);
WriteStringTo(requestStream, _lineBreak);
}

if (!UploadRaw)
WriteStringTo(requestStream, GetMultipartFooter());
}

Just need to change the header content-type because it seems it's adding multipart/form-data

Anyways just finish like soon, hope tomorrow, and I'll make a pull request....

Thanks for RestSharp
Reply all
Reply to author
Forward
0 new messages