<div dir="ltr">Hi Sven,<div><br></div><div style>thanks for the answers. My REST services are really simple, for example there is a list of users:</div><div style><br></div><div style><div><font face="courier new, monospace">GPRestfulHandler&gt;&gt;getUsers</font></div>
<div><font face="courier new, monospace"><br></font></div><div><font face="courier new, monospace"><span class="" style="white-space:pre">        </span>&lt;get&gt;</font></div><div><font face="courier new, monospace"><span class="" style="white-space:pre">        </span>&lt;path: &#39;user/list&#39;&gt;</font></div>
<div><font face="courier new, monospace"><span class="" style="white-space:pre">        </span>&lt;produces: &#39;application/json; charset=utf-8&#39;&gt;</font></div><div><font face="courier new, monospace"><span class="" style="white-space:pre">        </span>| instance container items |</font></div>
<div><font face="courier new, monospace"><span class="" style="white-space:pre">        </span>instance := GPUser new.</font></div><div><font face="courier new, monospace"><span class="" style="white-space:pre">        </span>container := instance descriptionId , instance descriptionLogin.</font></div>
<div><font face="courier new, monospace"><span class="" style="white-space:pre">        </span>items := self database getUsers.</font></div><div><font face="courier new, monospace"><span class="" style="white-space:pre">        </span>^ (items collect: [ :item | item asDictionaryFrom: container ]) asJson</font></div>
<div><br></div><div style>There is a JSON result:</div><div style><br></div><div style><pre style="color:rgb(0,0,0);word-wrap:break-word;white-space:pre-wrap"><font face="courier new, monospace">[{&quot;Id&quot;:&quot;688a7ymp9zn60qczvvyniprwq&quot;,&quot;Login&quot;:&quot;tomas&quot;},{&quot;Id&quot;:&quot;av0t1s9u4ddu747hd2xyfi58&quot;,&quot;Login&quot;:&quot;petr&quot;}]</font></pre>
</div><div style>Detail of user can be read by this method:</div><div style><br></div><div style><span style="font-family:&#39;courier new&#39;,monospace">GPRestfulHandler&gt;&gt;</span><font face="courier new, monospace">getUserByLogin: aLogin<br>
</font></div><div><br></div><div><font face="courier new, monospace"><span class="" style="white-space:pre">        </span>&lt;get&gt;</font></div><div><font face="courier new, monospace"><span class="" style="white-space:pre">        </span>&lt;path: &#39;user/detail/by-login/{aLogin}&#39;&gt;</font></div>
<div><font face="courier new, monospace"><span class="" style="white-space:pre">        </span>&lt;produces: &#39;application/json; charset=utf-8&#39;&gt;</font></div><div><font face="courier new, monospace"><span class="" style="white-space:pre">        </span>| user |</font></div>
<div><font face="courier new, monospace"><span class="" style="white-space:pre">        </span>user := self database getUserByLogin: aLogin.</font></div><div><font face="courier new, monospace"><span class="" style="white-space:pre">        </span>user isNil</font></div>
<div><font face="courier new, monospace"><span class="" style="white-space:pre">                </span>ifFalse: [ ^ user asJson ]</font></div><div><font face="courier new, monospace"><span class="" style="white-space:pre">                </span>ifTrue: [ self respondAsUserWasNotFindByLogin: aLogin ]</font></div>
<div style><br></div><div style>Result is here. I had to fix the failing ByteArray method to get GoalName with Czech characters:</div><div style><pre style="color:rgb(0,0,0);word-wrap:break-word;white-space:pre-wrap"><font face="courier new, monospace">{&quot;Id&quot;:&quot;688a7ymp9zn60qczvvyniprwq&quot;,&quot;Login&quot;:&quot;tomas&quot;,&quot;CurrentAccountBalance&quot;:12000,&quot;SavingsAccountReserve&quot;:0,&quot;SavingsAccountHeap&quot;:0,&quot;InterestRate&quot;:1,&quot;PaymentDay&quot;:15,&quot;SimulationDate&quot;:&quot;29 May 2013&quot;,&quot;SavingsAccountInterestReserve&quot;:0,&quot;SavingsAccountInterestHeap&quot;:0,&quot;Goals&quot;:[{&quot;GoalName&quot;:&quot;ěščřžýáíé-ĚŠČŘŽÝÁÍÉ&quot;,&quot;GoalAmount&quot;:8000,&quot;MaturityDate&quot;:&quot;29 November 2013&quot;}],&quot;Plans&quot;:[{&quot;PaymentDate&quot;:&quot;29 May 2013&quot;,&quot;PaymentAmount&quot;:1140.410292918764},{&quot;PaymentDate&quot;:&quot;29 June 2013&quot;,&quot;PaymentAmount&quot;:1140.410292918764},{&quot;PaymentDate&quot;:&quot;29 July 2013&quot;,&quot;PaymentAmount&quot;:1140.410292918764},{&quot;PaymentDate&quot;:&quot;29 August 2013&quot;,&quot;PaymentAmount&quot;:1140.410292918764},{&quot;PaymentDate&quot;:&quot;29 September 2013&quot;,&quot;PaymentAmount&quot;:1140.410292918764},{&quot;PaymentDate&quot;:&quot;29 October 2013&quot;,&quot;PaymentAmount&quot;:1140.410292918764},{&quot;PaymentDate&quot;:&quot;29 November 2013&quot;,&quot;PaymentAmount&quot;:1140.410292918764}]}</font><br>
</pre></div><div style><br></div><div style>Can you see any problem with the REST method?</div><div style><br></div><div style>Tomas</div></div><div style><br></div></div><div class="gmail_extra"><br><br><div class="gmail_quote">
On Wed, Jun 26, 2013 at 2:00 PM, Sven Van Caekenberghe <span dir="ltr">&lt;<a href="mailto:sven@stfx.eu" target="_blank">sven@stfx.eu</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Hi Tomas,<br>
<div class="im"><br>
On 26 Jun 2013, at 13:20, Tomas Kukol &lt;<a href="mailto:tomas.kukol@gmail.com">tomas.kukol@gmail.com</a>&gt; wrote:<br>
<br>
&gt; Hi Sven,<br>
&gt;<br>
&gt; you are right. Seaside needs it as a string. But I&#39;ve found another problem with output (maybe because Seaside uses string???) which leads to error &#39;Improper store into indexable object&#39;.<br>
&gt;<br>
&gt; I have JSON on output. It is again string and there is this problem. In ZnZincServerAdaptor&gt;&gt;responseFrom: we are asking for contents.<br>
<br>
</div>I don&#39;t know much about Seaside-REST but I would guess, from my general understanding how Seaside works, that if you generate output, like JSON, your output has to pass through an encoder before it is stored as bytes (well actually a ByteString, but that is another story) inside RWBinaryOrTextStream&#39;s contents of the WABufferedResponse. The encoder will make sure WideCharacters get encoded properly and then the #replaceFrom:to:with:startingAt: will work (and magically bridge the ByteArray/ByteString gap).<br>

<br>
So my guess is that you either have not set the proper encoder on the response when writing output to it, or you write the output the wrong way.<br>
<br>
There has to be a codec stream in between. See<br>
<br>
WAServerAdaptor&gt;&gt;#responseFor:<br>
GRPharoUtf8Codec&gt;&gt;#encoderFor:<br>
<br>
HTH,<br>
<br>
Sven<br>
<div class="HOEnZb"><div class="h5"><br>
&gt; ZnZincServerAdaptor&gt;&gt;responseFrom: aRequestContext<br>
&gt;       | partialHeaders cookies fullHeaders seasideResponse contents entity contentType |<br>
&gt;       seasideResponse := aRequestContext response.<br>
&gt;       partialHeaders := seasideResponse headers.<br>
&gt;       cookies := seasideResponse cookies.<br>
&gt;       fullHeaders := ZnHeaders defaultResponseHeaders.<br>
&gt;       partialHeaders keysAndValuesDo: [ :key :value |<br>
&gt;               fullHeaders at: key put: value ].<br>
&gt;       cookies do: [ :each |<br>
&gt;               fullHeaders at: &#39;Set-Cookie&#39; add: each oldNetscapeString.<br>
&gt;               fullHeaders at: &#39;Set-Cookie2&#39; add: each rfc2965String ].<br>
&gt;       contentType := seasideResponse contentType greaseString.<br>
&gt;       contents := seasideResponse contents. &quot;&lt;------------------------ HERE - we are asking for contents&quot;<br>
&gt;       entity := (ZnEntity bytes: contents) contentType: contentType; yourself.<br>
&gt;       ^ ZnResponse new<br>
&gt;               statusLine: (ZnStatusLine code: seasideResponse status);<br>
&gt;               headers: fullHeaders;<br>
&gt;               entity: entity;<br>
&gt;               yourself<br>
&gt;<br>
&gt; It calls:<br>
&gt;<br>
&gt; WABufferedResponse&gt;&gt;contents<br>
&gt;       ^ contentsStream contents &quot;&lt;------------------------ HERE - normally, asking underlying stream for contents&quot;<br>
&gt;<br>
&gt; An the error is coming here:<br>
&gt;<br>
&gt; RWBinaryOrTextStream&gt;&gt;contents<br>
&gt;       &quot;Answer with a copy of my collection from 1 to readLimit.&quot;<br>
&gt;<br>
&gt;       | newArray |<br>
&gt;       isBinary ifFalse: [^ super contents].   &quot;String&quot;<br>
&gt;       readLimit := readLimit max: position.<br>
&gt;       newArray := ByteArray new: readLimit. &quot;&lt;------------------------ HERE - we are creating array of bytes (SmallInteger 0 - 255)&quot;<br>
&gt;       ^ newArray replaceFrom: 1<br>
&gt;               to: readLimit<br>
&gt;               with: collection  &quot;&lt;------------------------ HERE - but we are having WideString in collection - characters&quot;<br>
&gt;               startingAt: 1.<br>
&gt;<br>
&gt; Now we are calling primitive 105:<br>
&gt;<br>
&gt; ByteArray&gt;&gt;replaceFrom: start to: stop with: replacement startingAt: repStart<br>
&gt;       &quot;Primitive. This destructively replaces elements from start to stop in the receiver starting at index, repStart, in the collection, replacement. Answer the receiver. Range checks are performed in the primitive only. Optional. See Object documentation whatIsAPrimitive.&quot;<br>

&gt;       &lt;primitive: 105&gt;<br>
&gt;       super replaceFrom: start to: stop with: replacement startingAt: repStart &quot;&lt;------------------------ HERE - it fails and continues here&quot;<br>
&gt;<br>
&gt; It calls loop in SequenceableCollection:<br>
&gt;<br>
&gt; ByteArray(SequenceableCollection)&gt;&gt;replaceFrom: start to: stop with: replacement startingAt: repStart<br>
&gt;       &quot;This destructively replaces elements from start to stop in the receiver<br>
&gt;       starting at index, repStart, in the sequenceable collection,<br>
&gt;       replacementCollection. Answer the receiver. No range checks are<br>
&gt;       performed.&quot;<br>
&gt;<br>
&gt;       | index repOff |<br>
&gt;       repOff := repStart - start.<br>
&gt;       index := start - 1.<br>
&gt;       [(index := index + 1) &lt;= stop]<br>
&gt;               whileTrue: [self at: index put: (replacement at: repOff + index)] &quot;&lt;------------------------ HERE - here we are putting character into ByteArray&quot;<br>
&gt;<br>
&gt; Stops here at Object at primitive 61:<br>
&gt;<br>
&gt; ByteArray(Object)&gt;&gt;at: index put: value<br>
&gt;       &quot;Primitive. Assumes receiver is indexable. Store the argument value in<br>
&gt;       the indexable element of the receiver indicated by index. Fail if the<br>
&gt;       index is not an Integer or is out of bounds. Or fail if the value is not of<br>
&gt;       the right type for this kind of collection. Answer the value that was<br>
&gt;       stored. Essential. See Object documentation whatIsAPrimitive.&quot;<br>
&gt;<br>
&gt;       &lt;primitive: 61&gt;<br>
&gt;       index isInteger ifTrue:<br>
&gt;               [self class isVariable<br>
&gt;                       ifTrue: [(index &gt;= 1 and: [index &lt;= self size])<br>
&gt;                                       ifTrue: [self errorImproperStore] &quot;&lt;------------------------ HERE - ends here&quot;<br>
&gt;                                       ifFalse: [self errorSubscriptBounds: index]]<br>
&gt;                       ifFalse: [self errorNotIndexable]].<br>
&gt;       index isNumber<br>
&gt;               ifTrue: [^self at: index asInteger put: value]<br>
&gt;               ifFalse: [self errorNonIntegerIndex]<br>
&gt;<br>
&gt; I&#39;ve seen this problem mostly with Seaside-REST, but also in some other posts from Seaside.<br>
&gt;<br>
&gt; Any idea how to fix it?<br>
&gt;<br>
&gt; Regards,<br>
&gt; Tomas<br>
&gt;<br>
&gt;<br>
&gt; On Wed, Jun 26, 2013 at 12:17 AM, Sven Van Caekenberghe &lt;<a href="mailto:sven@stfx.eu">sven@stfx.eu</a>&gt; wrote:<br>
&gt; Hi Tomas,<br>
&gt;<br>
&gt; Thanks for the report: this is an important area indeed.<br>
&gt;<br>
&gt; However, the comment says it: this is intentional, and as far as I know, it is correct. You see, Zinc will correctly decode/encode any HTTP payload, using the supplied mime-type and/or charsets. Seaside is written such that it does not want this: it insists on doing this on its own. That is why ZnZincServerAdaptor is _not_ using the normal request reading code of Zn, but uses a special option to read everything binary. The stupid thing is that even though Seaside needs bytes, it wants them as a String. That is the reason for the otherwise brain dead #asString (and the implicit copy is inefficient as well).<br>

&gt;<br>
&gt; Of course, you will see your special characters there if you do UTF8 decoding, but that is because you already know what is inside.<br>
&gt;<br>
&gt; What normally happens, is that later on in the processing, Seaside will access the WARequest payload using proper decoding, using its own framework (much like what Zn would do). AFAIK this whole process works. You can actually test this using some of the functional tests.<br>

&gt;<br>
&gt; I am not sure that Seaside-REST is doing the right thing (there were some issues with SmalltalkHub as well), but I would think so.<br>
&gt;<br>
&gt; Are you sure you have set the correct encoding on the adaptor ?<br>
&gt;<br>
&gt; Are you sure you are posting as application/json;charset=utf-8 and if you do not set the charset, are you sure utf-8 is the default ?<br>
&gt;<br>
&gt; Are you sure your REST handler and/or JSON parser does the right thing ?<br>
&gt;<br>
&gt; It is too late right now, but if we want to get further with this, I will need a failing unit test - if these exist in Seaside-REST, but I would assume so. I have no experience running Seaside-REST, I am using Zinc-REST myself, but I would like to learn.<br>

&gt;<br>
&gt; Regards,<br>
&gt;<br>
&gt; Sven<br>
&gt;<br>
&gt; On 25 Jun 2013, at 23:24, Tomas Kukol &lt;<a href="mailto:tomas.kukol@gmail.com">tomas.kukol@gmail.com</a>&gt; wrote:<br>
&gt;<br>
&gt; &gt; Hi Sven.<br>
&gt; &gt;<br>
&gt; &gt; I&#39;ve had a problem when POSTing non-ascii UTF-8 characters in JSON to Seaside REST service. I&#39;ve located the problem in the method ZnZincServerAdaptor&gt;&gt;requestBodyFor: where the body of ZnRequest is translated to body of WARequest. I use Pharo 1.4 with Seaside 3.0.8 and Zinc-Seaside-SvenVanCaekenberghe.40.<br>

&gt; &gt;<br>
&gt; &gt; When the POSTed JSON contains non-ascii UTF-8 characters (Czech characters), they are corrupted. The problem is on the &quot;MARKED&quot; line, where the array of bytes changed to string by asString.<br>
&gt; &gt;<br>
&gt; &gt; &quot;Problematic&quot; code:<br>
&gt; &gt;<br>
&gt; &gt; ZnZincServerAdaptor&gt;&gt;requestBodyFor: aZincRequest<br>
&gt; &gt;       ^ (aZincRequest method ~= #TRACE<br>
&gt; &gt;               and: [ aZincRequest hasEntity<br>
&gt; &gt;                       and: [ aZincRequest entity isEmpty not<br>
&gt; &gt;                               and: [ (aZincRequest entity contentType matches: ZnMimeType applicationFormUrlEncoded) not<br>
&gt; &gt;                                       and: [ (aZincRequest entity contentType matches: ZnMimeType multiPartFormData) not ] ] ] ])<br>
&gt; &gt;                       ifTrue: [<br>
&gt; &gt;                               &quot;Seaside wants to do its own text conversions&quot;<br>
&gt; &gt;                               aZincRequest entity bytes asString &quot;MARKED&quot; ]<br>
&gt; &gt;                       ifFalse: [<br>
&gt; &gt;                               String new ]<br>
&gt; &gt;<br>
&gt; &gt; I did a quick correction, which is not nice, but works for me:<br>
&gt; &gt;<br>
&gt; &gt; ZnZincServerAdaptor&gt;&gt;requestBodyFor: aZincRequest<br>
&gt; &gt;       ^ (aZincRequest method ~= #TRACE<br>
&gt; &gt;               and: [ aZincRequest hasEntity<br>
&gt; &gt;                       and: [ aZincRequest entity isEmpty not<br>
&gt; &gt;                               and: [ (aZincRequest entity contentType matches: ZnMimeType applicationFormUrlEncoded) not<br>
&gt; &gt;                                       and: [ (aZincRequest entity contentType matches: ZnMimeType multiPartFormData) not ] ] ] ])<br>
&gt; &gt;                       ifTrue: [<br>
&gt; &gt;                               &quot;Seaside wants to do its own text conversions&quot;<br>
&gt; &gt;                               ZnUTF8Encoder new decodeBytes: aZincRequest entity bytes &quot;CORRECTED&quot; ]<br>
&gt; &gt;                       ifFalse: [<br>
&gt; &gt;                               String new ]<br>
&gt; &gt;<br>
&gt; &gt; My correction tries to decode byte array with ZnUTF8Encoder and the result is OK.<br>
&gt; &gt;<br>
&gt; &gt; Maybe I would recommend to use GRPharoUtf8Codec (although I like ZnUTF8Encoder more) or even better self codec (self = ZnZincServerAdaptor) to try to decode the bytes.<br>
&gt; &gt;<br>
&gt; &gt; Regards,<br>
&gt; &gt; Tomas Kukol<br>
&gt; &gt; _______________________________________________<br>
&gt; &gt; seaside mailing list<br>
&gt; &gt; <a href="mailto:seaside@lists.squeakfoundation.org">seaside@lists.squeakfoundation.org</a><br>
&gt; &gt; <a href="http://lists.squeakfoundation.org/cgi-bin/mailman/listinfo/seaside" target="_blank">http://lists.squeakfoundation.org/cgi-bin/mailman/listinfo/seaside</a><br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt; --<br>
&gt; Sven Van Caekenberghe<br>
&gt; <a href="http://stfx.eu" target="_blank">http://stfx.eu</a><br>
&gt; Smalltalk is the Red Pill<br>
&gt;<br>
&gt; _______________________________________________<br>
&gt; seaside mailing list<br>
&gt; <a href="mailto:seaside@lists.squeakfoundation.org">seaside@lists.squeakfoundation.org</a><br>
&gt; <a href="http://lists.squeakfoundation.org/cgi-bin/mailman/listinfo/seaside" target="_blank">http://lists.squeakfoundation.org/cgi-bin/mailman/listinfo/seaside</a><br>
&gt;<br>
&gt; _______________________________________________<br>
&gt; seaside mailing list<br>
&gt; <a href="mailto:seaside@lists.squeakfoundation.org">seaside@lists.squeakfoundation.org</a><br>
&gt; <a href="http://lists.squeakfoundation.org/cgi-bin/mailman/listinfo/seaside" target="_blank">http://lists.squeakfoundation.org/cgi-bin/mailman/listinfo/seaside</a><br>
<br>
_______________________________________________<br>
seaside mailing list<br>
<a href="mailto:seaside@lists.squeakfoundation.org">seaside@lists.squeakfoundation.org</a><br>
<a href="http://lists.squeakfoundation.org/cgi-bin/mailman/listinfo/seaside" target="_blank">http://lists.squeakfoundation.org/cgi-bin/mailman/listinfo/seaside</a><br>
</div></div></blockquote></div><br></div>