Problems with multipart forms

535 views
Skip to first unread message

phil

unread,
Jan 13, 2012, 1:59:45 PM1/13/12
to RestSharp
I am trying to use RestSharp to upload a csv to a web service but I am
running into an error. I have tried googiling the issue but all I
found was an old post that seems unrelated. If anyone has insight into
what I am doing wrong I would greatly appreciate it. I see the
following error:

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


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);
}
}

I am using the following code
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 =
"<platform>" +
"<bulk>" +
"<mapping>" + mapName + "</mapping>"
+
"</bulk>" +
"</platform>";

request.AddParameter("application/xml", importXml,
ParameterType.RequestBody);
request.AddFile(fileName, fileNameAndPath);

RestResponse response = client.Execute(request);
string content = response.Content;
}

Thank you,
-Phil

John Sheehan

unread,
Jan 19, 2012, 1:01:05 AM1/19/12
to rest...@googlegroups.com
This definitely seems like a bug. Will look into it. Did you open an issue on Github for it? If not, can you? https://github.com/restsharp/RestSharp/issues

phil

unread,
Jan 19, 2012, 4:05:32 PM1/19/12
to RestSharp
Thanks for your help. I created defect #204.

-Phil

Evandro da Silva Amparo

unread,
Mar 19, 2012, 2:16:17 PM3/19/12
to RestSharp
Any update on this issue? I'm trying to build a client that depends on
it.

Jesse Smith

unread,
May 23, 2013, 4:42:11 PM5/23/13
to rest...@googlegroups.com
Having same issue anytime I want to use AddBody and AddFile together.

asir...@gmail.com

unread,
Jun 4, 2013, 5:45:41 PM6/4/13
to rest...@googlegroups.com
Has there been a fix for this issue?

Khalid Abuhakmeh

unread,
Aug 5, 2013, 9:01:37 AM8/5/13
to rest...@googlegroups.com
Still having the same issue as well, is there a workaround?
Reply all
Reply to author
Forward
0 new messages