2

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

Kim Brandl
  • 13,125
  • 2
  • 16
  • 21
  • This question appears to be off-topic because it is not actually a question. Maybe you meant to contact "Smartsheet" directly? – nobody Apr 17 '14 at 15:41
  • Sure. Actually, my question is whether anyone else is seeing this? I find it tough to believe that the C# SDK was released with this basic a bug, because it breaks all posts. So perhaps I have the wrong version of one of the dependent libraries like RestSharp? – Praveen Seshadri Apr 17 '14 at 22:28
  • We were definitely able to reproduce this on version 1.0.1 and recommend upgrading to 1.0.2 to resolve this issue. – Brett Apr 17 '14 at 23:06
  • 1
    Thanks Brett. I'm glad you replied because I was going to try uploading binary files next and would have run into the problem you identified. I'll apply your diffs and move forward and will switch back to the NuGet packages sometime soon. – Praveen Seshadri Apr 18 '14 at 04:49

1 Answers1

1

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:

  • Fixed issue with post requests where content body was not handled correctly.
  • Added index to the ColumnToSheetBuilder class.
  • Added filename when uploading an attachment.

Techinical Details

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();
                        }
                }
        }
Brett
  • 2,502
  • 19
  • 30