Creating and sending a JSON Object for POST HTTP Request


#1

1) Give a description of the problem
I’m using Make a web request to send a POST to a webservice (nzbget’s JSON-RPC service if you’re wondering). An example request is:
{method: "rate", params: [123]}

That’s essentially a json object with two keys, one has a string value and the other is an array of strings (just one value at position 0).

Plan is to pass this as the content body/send variables

2) What is the expected behavior?
I need a good way to define this in webCoRE, have tried:

  • Trying to create a string which happens to be formatted as a JSON object - had a hard time escaping the doublequotes.
  • Creating an array/list with keys as position names and values - but I can’t then pass that array
  • Just passing arguments and triggering from another piston - also failed, was an early attempt.

3) What is happening/not happening?
See above

**4) Post a Green Snapshot of the piston!
This is in an early stage, manually triggered for now.

Eventually I’d like to be able to call that from other pistons with a simple argument that gets passed as the rate (e.g. Everyone leaves, set rate to 3500)

5) Attach any logs (From ST IDE and by turning logging level to Full)
+1ms ╔Received event [Home].time = 1508364426618 with a delay of -1332ms +204ms ║RunTime Analysis CS > 30ms > PS > 102ms > PE > 71ms > CE +216ms ║Runtime (38878 bytes) successfully initialized in 102ms (v0.2.0fa.20171011) (215ms) +217ms ║╔Execution stage started +243ms ║║Sent to NZBget: [method:rate, params:1234] +244ms ║║Executed virtual command log (1ms) +253ms ║║Code: 408 +254ms ║║Executed virtual command log (1ms) +256ms ║╚Execution stage complete. (39ms) +265ms ╚Event processed successfully (265ms)

Note about the event at 243ms: That wasn’t actually sent to nzbget (look closely at the code, at the time of screenshotting, nothing was being sent)

Have been both googling and searching, including getting the idea about arrays from the recent thread in Meta about it, something kind of on the smartthings forums (can’t link, new users can only post 2 links) and this on the same subject but not using JSON objects.


#2

the post doesnt seem to sending any data? a post with data should look like this:

heres a param string formatted properly:


#3

Yep, knew it wasn’t sending any in the screenshot. Wish I had corrected it before putting it on here. However, I was finding I couldn’t send or even select an array/list (e.g. nzbget_request) couldn’t be used:

I think I have it working but the API isn’t responding as expected, keep in mind I don’t think this is webcore issue:


#4

though all is not working yet sounds like the webcore part of it is now working as expected?


#5

Yep agreed. I’ll see if I can get it working then post my set NZBget rate based on presence code.


#6

Not sure this is the right place to ask this…and I am very new to JSON RPC.

This HTTP request will start playing my Christian.xsp playlist in Kodi.

http://192.168.0.51:8081/jsonrpc?request={“jsonrpc”:“2.0”,“id”:“1”,“method”:“Player.Open”,“params”:{“item”:{“file”:“special://profile/playlists/music/Christian.xsp”}}}

Can I use POST within webcore with this to start playing any xxx.xsp playlist?


#7

If I put the “Working_JSON_String” into my browser, it works perfectly. I tried the piston below with a local IP and my external IP. I set up port forwarding for port 8081. It did not work. Any suggestions?


#8

Send the data to requestcatcher.com and see what webcore is sending. Also, try urlEncode() on the data you’re transmitting, browsers probably do it automatically.


#9

Ok, I TRIED to make sense of what you suggested. I am so new to JSON. Can you spoon feed me a little more?


#10

Ok, when you go to requestcatcher, you can set up a URL for where you direct your web request… let’s just say you picked https://pantheon.requestcatcher.com/, substitute that to the domain name in your webcore web request and watch the data https://pantheon.requestcatcher.com/ receives, to see if it is coming properly formatted.


#11

I think I understand now.
Webcore is sending this (from requestcatcher.com) :

{“jsonrpc”:"?request={“jsonrpc”:“2.0”,“id”:“1"”,“method”:“method”:“Player.Open”,“params”:"“params”:{“item”:{“file”:“special://profile/playlists/music/80’s hits.xsp”}}}"}

And if I input the string into my browser I get this from requestcatcher.com :

GET /jsonrpc?request={%22jsonrpc%22:%222.0%22,%22id%22:%221%22,%22method%22:%22Player.Open%22,%22params%22:{%22item%22:{%22file%22:%22special://profile/playlists/music/80%27s%20hits.xsp%22}}}


#12

Sorry, Webcore is sending this :

{"jsonrpc":"?request={\"jsonrpc\":\"2.0\",\"id\":\"1\"","method":"method\":\"Player.Open","params":"\"params\":{\"item\":{\"file\":\"special://profile/playlists/music/80's hits.xsp\"}}}"}

#13

This is not working…


#14

Use GET rather than POST and create a string variable called request. To that variable, assign exactly the following value:

\{"jsonrpc":"2.0","id":"1","method":"Player.Open","params":\{"item":\{"file":"special://profile/playlists/music/80’s hits.xsp"\}\}\}

Send this request variable with your GET to http://192.168.0.51:8081/jsonrpc. webCore will append the ?request=... portion and do any URL encoding that may be necessary.

The \{ in the value above is required when writing JSON as text in order to avoid conflicts with webCoRE’s {someVariable} expression syntax. Also be careful with the playlist name, you may need to double check whether it is actually 80’s hits.xsp with a curly apostrophe or 80's hits.xsp with a straight single quote.


Feature Request: Piston $return variable
#15

@ipaterson
THANK YOU, THANK YOU, THANK YOU !!!

Literally, I have spent MONTHS searching the web, pouring through posts, digesting wiki, trying numerous examples of code , asking questions, learning webCoRE, JSON, and how to interface with an API…just to accomplish the single task of passing a playlist name from webCoRE to Kodi. Your tips here were a beacon of light that got me through.
I had no idea that webCoRE appended the ?request= part. Your help with the variable structure nailed it for me. Three things :

  1. You were totally correct about the apostrophe in the “80’s hits” part. Curly versus straight apostrophe makes a difference.
  2. I did have to use POST instead of GET.
  3. You must use your PUBLIC IP and port to access the Kodi API from webCoRE (ie, SmartThings). Thanks @bangali.

Yes, you can say “Alexa, tell Kodi to play the playlist 80’s hits.” And it will play. But I needed this approach.


#16

Is there anyway you’d be willing to share the anonymized snapshot so can get the code? Does it matter what you’re running Kodi on whether it’s Windows, Android, Raspberry Pi, etc.?


#17

I don’t know why there is junk in this image in the variables area. It does not matter though. That string really is not important. The “result” string is the key to the piston. I am running Kodi on a RPi 3 B+. See here to get an idea of the different file locations within Kodi with respect to your OS. Make sure to use your PUBLIC IP (not the IP of Kodi on your LAN) and that you have your Kodi port forwarded so you can access the API from webCoRE. Lastly, I am an experienced NOVICE at this. To accomplish this task required pulling information together that I do not completely understand. I am willing to help anyone, but certainly I am not an expert here.


#18

I wonder if this is related to a bug with local requests that is fixed for the next release

Currently POST requests on the local network encode FORM values as JSON and the CUSTOM request is always identified as XML rather than the type that you select. That would probably confuse Kodi unless it is extremely lenient.


#19

I kinda understand this. Does this mean that the next release will break my current piston?


#20

@Pantheon so all I’m going to do is change the file location for where playlists are stored in both variables right? Use the location/playlist name.xsp