restsharp/RestSharp — Issue #204: Problems with multipart forms
Author: pwiesner
Created: 2012-01-19 21:01:30+00:00
State: open

Hi,

I originally posted this on the RestSharp google group and was asked to add an issue here as well.

I am trying to use RestSharp to upload a csv to a web service but I am running into an error. RestSharp crashes if the request has both a body and an attached file. I am not sure if I am doing something incorrectly or if this is a bug.

An abbreviated example:

static void UploadFile(string baseUrl, string sessionId, string arpt, string objectName, string mapName, string fileName, string fileNameAndPath)
{
RestClient client = new RestClient(baseUrl);
RestRequest request = new RestRequest("bulk/ObjectName", Method.POST);
client.UserAgent = _userAgent;
request.AddCookie("JSESSIONID", sessionId);
request.AddCookie("ARPT", arpt);
String importXml =
"" +
"" +
"" + mapName + "" +
"" +
"";
request.AddParameter("application/xml", importXml, ParameterType.RequestBody);
request.AddFile(fileName, fileNameAndPath);
RestResponse response = client.Execute(request);
string content = response.Content;
}

Error and stack trace:

This property cannot be set after writing has started.
at System.Net.HttpWebRequest.set_ContentLength(Int64 value)
at RestSharp.Http.WriteRequestBody(HttpWebRequest webRequest) in C:\restsharp-RestSharp-109cf8e\restsharp-RestSharp-109cf8e\RestSharp\Http.Sync.cs:line 175
at RestSharp.Http.PostPutInternal(String method) in C:\restsharp-RestSharp-109cf8e\restsharp-RestSharp-109cf8e\RestSharp\Http.Sync.cs:line 103
at RestSharp.Http.Post() in C:\restsharp-RestSharp-109cf8e\restsharp-RestSharp-109cf8e\RestSharp\Http.Sync.cs:line 39
at RestSharp.RestClient.GetResponse(IRestRequest request) in C: \restsharp-RestSharp-109cf8e\restsharp-RestSharp-109cf8e\RestSharp\RestClient.Sync.cs:line 88
at RestSharp.RestClient.Execute(IRestRequest request) in C:\restsharp-RestSharp-109cf8e\restsharp-RestSharp-109cf8e\RestSharp\RestClient.Sync.cs:line 47

>	RestSharp.dll!RestSharp.Http.WriteRequestBody(System.Net.HttpWebRequest webRequest) Line 170	C#
RestSharp.dll!RestSharp.Http.PostPutInternal(string method) Line 103 + 0xb bytes	C#
RestSharp.dll!RestSharp.Http.Post() Line 39 + 0xe bytes	C#
RestSharp.dll!RestSharp.RestClient.GetResponse(RestSharp.IRestRequest request) Line 88 + 0x9 bytes	C#
RestSharp.dll!RestSharp.RestClient.Execute(RestSharp.IRestRequest request) Line 47 + 0xb bytes	C#
MultiPartTest.exe!MultiPartTest.Program.Login(string baseUrl, string username, string password) Line 73 + 0xe bytes	C#
MultiPartTest.exe!MultiPartTest.Program.GetSessionState(string baseUrl, string username, string password) Line 43 + 0xe bytes	C#
MultiPartTest.exe!MultiPartTest.Program.Main(string[] args) Line 23 + 0x17 bytes	C#

The exception is thrown when setting webRequest.ContentLength.

private void WriteRequestBody(HttpWebRequest webRequest)
{
if (!HasBody)
return;

var bytes = _defaultEncoding.GetBytes(RequestBody);

webRequest.ContentLength = bytes.Length;

using (var requestStream = webRequest.GetRequestStream())
{
requestStream.Write(bytes, 0, bytes.Length);
}
}

Thank you,
-Phil