I'm using the C# SDK and it seems that there is a bug when executing any POST. The post body is serialized as the string "System.IO.StringReader". This seems to be because of line 127 in Internal/Http/DefaultHttpClient.cs
I changed the code from:
restRequest.AddParameter("application/json", smartsheetRequest.Entity.GetContent(),ParameterType.RequestBody);
to:
restRequest.AddParameter("application/json", smartsheetRequest.Entity.GetContent().ReadToEnd(),ParameterType.RequestBody);
and it seems to fix the problem. Could someone from Smartsheet check and confirm please?
Thx
Thanks for bringing this to our attention! We were able to include the fix for this in a release that we pushed out today. Just upgrade your package using NuGet or download the latest source code from Github.
The list of changes for this release are:
This issue was introduced on March 17th two days before we released version 1.0.1 and specifically was caused by the commit 2d69ef5b8f95dbe911d9bb1ecb50a6a441b522b5 where the ReadToEnd() method was removed in order to support binary attachments.
This issue was caused by the line that you pointed to where the smartsheetRequest.Entity.GetContent()
was allowing the restRequest object to call ToString() which gave us a request body like the following:
POST https://api.smartsheet.com/1.1/home/folders HTTP/1.1
Authorization: Bearer THE_TOKEN
Accept: application/json, application/xml, text/json, text/x-json, text/javascript, text/xml
User-Agent: smartsheet-csharp-sdk(sdk-csharp-sample)/0.0.0.1 Microsoft Windows 7 Enterprise
Content-Type: application/json
Host: api.smartsheet.com
Content-Length: 22
Accept-Encoding: gzip, deflate
System.IO.StreamReader
The last line should have been the real content of the StreamReader not the ToString() of the StreamReader.
The solution you mentioned of using ReadToEnd() on the StreamReader was a good one except that it does not handle binary data. We implemented this change by using the GetBinaryContent()
method. Then we converted it to a byte array by using Util.ReadAllBytes(...)
.
The exact changes are listed below in diff format:
diff --git a/main/Smartsheet/Api/Internal/Http/DefaultHttpClient.cs b/main/Smartsheet/Api/Internal/Http/DefaultHttpClient.cs
index 5913935..df6d7d5 100644
--- a/main/Smartsheet/Api/Internal/Http/DefaultHttpClient.cs
+++ b/main/Smartsheet/Api/Internal/Http/DefaultHttpClient.cs
@@ -121,10 +121,10 @@ namespace Smartsheet.Api.Internal.Http
restRequest.AddHeader(header.Key, header.Value);
}
}
-
+
if (smartsheetRequest.Entity != null && smartsheetRequest.Entity.GetContent() != null)
{
- restRequest.AddParameter("application/json", smartsheetRequest.Entity.GetContent(),
+ restRequest.AddParameter("application/json", Util.ReadAllBytes(smartsheetRequest.Entity.GetBinaryContent()),
ParameterType.RequestBody);
}
diff --git a/main/Smartsheet/Api/Internal/Util/Util.cs b/main/Smartsheet/Api/Internal/Util/Util.cs
index ee97b41..c3d48c6 100644
--- a/main/Smartsheet/Api/Internal/Util/Util.cs
+++ b/main/Smartsheet/Api/Internal/Util/Util.cs
@@ -85,8 +85,11 @@ namespace Smartsheet.Api.Internal.Utility
byte[] buffer = new byte[bufferSize];
int count;
while ((count = reader.Read(buffer, 0, buffer.Length)) != 0)
+ {
ms.Write(buffer, 0, count);
- return ms.ToArray();
+ }
+ ms.Position = 0;
+ return ms.ToArray();
}
}
}