Problem using curly brace { in a json string as http request

Post your questions and help other users.

Moderator: Martin

Post Reply
vfn
Posts: 4
Joined: 15 Feb 2014 01:43

Problem using curly brace { in a json string as http request

Post by vfn » 15 Feb 2014 02:07

I need to send something via http request like this:

http://emoncms.org/input/post.json?json ... ba34gh3456

I can build the expression no problem, but where I'm having trouble is the curly braces. I can build the expression as a variable or the contents of a file. The emoncms input requires them, and Automagic keeps trying to interpret them. I have read through the forums and looked for help already. This thread was useful:

http://automagic4android.com/forum/view ... f=5&t=2544

I've tried all kinds of combinations of quote marks, etc, but I still can't make it work.

Any suggestions?

Thanks

User avatar
Martin
Posts: 4468
Joined: 09 Nov 2012 14:23

Re: Problem using curly brace { in a json string as http req

Post by Martin » 15 Feb 2014 06:55

You can use the + operator in a script to compose the URL. I recommend to create the JSON-text in an action script:

Code: Select all

variable1="text1";
variable2="text2";
json = '{var1:' + variable1 + ',var2:' + variable2 + '}';
URL in action HTTP Request:
http://emoncms.org/input/post.json?json ... ba34gh3456

The Android libraries to handle URLs are very strict so above URL will not work because Android does not accept the {-character of the JSON text in the query part of the URL. Special characters need to be escaped with the encodeURL-function:
http://emoncms.org/input/post.json?json ... ba34gh3456
This will also encode all other special characters that might be contained in the variables embedded in the JSON string.

Theoretically you could also create the URL in an inline script directly in action HTTP Request (it's not very readable in this case):
http://emoncms.org/input/post.json?json={encodeURL('{var1:' + variable1 + ',var2:' + variable2 + '}')}&apikey=beshytb5fd0633c8d7d24dba34gh3456

Regards,
Martin

vfn
Posts: 4
Joined: 15 Feb 2014 01:43

Re: Problem using curly brace { in a json string as http req

Post by vfn » 15 Feb 2014 18:59

Martin,

Thank you VERY much!!!

I followed your instructions to the letter and it now works perfectly.

Thanks again.

Post Reply