JSON, URL-encode, and UTF-8
Charles Warwick
charles at techstrategies.com.au
Fri Aug 21 04:07:11 EDT 2015
HI Peter,
I did some testing with Jacque and found that her LC7 was sending the wrong content-type when posting to the PHP script.
In case anyone is interested:
It was using a content type of 'application/json’ which would have made sense if the PHP script was written to accept the json data directly.
However, it was expecting the POST data to be encoded in name/value format: variableA=valueA&variableB=valueB.
In this case, it required the json data stored in a variable called json: i.e. json=<json data>
Changing the headers as follows before the POST resolved the issue:
set the httpHeaders to “content-type: application/x-www-form-urlencoded"
As I mentioned to Jacque, the json data itself needs to be urlEncoded.
Cheers,
Charles
> On 21 Aug 2015, at 5:20 pm, Peter W A Wood <peterwawood at gmail.com> wrote:
>
>
>> On 21 Aug 2015, at 10:59, Peter W A Wood <peterwawood at gmail.com> wrote:
>>
>> Jacque
>>
>>> On 21 Aug 2015, at 10:13, J. Landman Gay <jacque at hyperactivesw.com <mailto:jacque at hyperactivesw.com>> wrote:
>>>
>>> Here's a test. This posts just fine in LC 6.x but fails in 7.0.6 and 7.1rc1.
>>>
>>> Make a field with this in it:
>>>
>>> [{"Meals":"Yes","Purpose":"Business"}]
>>>
>>> Now put this into a button or card script:
>>>
>>> constant kServerURL = "https://www.domain.com/results.php <https://www.domain.com/results.php>" --### use a valid URL here
>>>
>>>
>>> command sendToServer
>>> if the version < 7 then
>>> put fld 1 into tData
>>> put "json=" before tData
>>> else
>>> put textEncode(fld 1,"UTF8") into tData
>>> put textEncode("json=",UTF8) before tData
>>> end if
>>> post tData to kServerURL
>>> put it into tResponse
>>> put the result into tErr
>>> breakpoint -- so you can see it
>>> end sendToServer
>>>
>>> If someone can test this against a simple echo PHP maybe it will tell us something. The real server returns empty in LC 7 and "OK" in LC 6.
>>
>> This works fine in LiveCode 7.0.3. I’ll download 7.1RC1 and see if it works there too.
>>
>> Peter
>>
>
> It also works on LiveCode 7.1 RC1 … which took me hours to download :-(
>
> Regards
>
> Peter
>
> _______________________________________________
> use-livecode mailing list
> use-livecode at lists.runrev.com
> Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
> http://lists.runrev.com/mailman/listinfo/use-livecode
More information about the use-livecode
mailing list