+1

POST requests with WIRE

thillsman 11 lat temu zaktualizowano 11 lat temu 2
I've got a broad, simple question about sending POST requests with WIRE. (I'm pretty new to REST APIs: I know how to send HTML forms to PHP, I've played with John's excellent Parse example, and I really get GETs. However, POST requests are a little tricky.)

Here's what I've got:

I made a super simple php script that lives at http://tylr.us/testing/posttest.php. Here's the code:
<?php
<br/>
header('Content-Type: application/json');
<br/>
$data = array("key" => $_POST["value"]);
<br/>
echo json_encode($data);
<br/>
?>
This script does three things: gives itself a content-type of JSON, creates an array and adds a value that is POSTed as "value" to it, and encodes the array as JSON. I've been testing it with the Postman plug-in for Chrome and everything looks beautiful:



Now I'm trying to do two things from WIRE: pass the value through POST, and display the output. Super simple (I hope). Here's what I'm trying to do in WIRE:

<wire><datasources><datasource name="posttest" source="" query="//" httpmethod="post" providertype="json" /></datasources><actions><action name="posttest" oninit="yes"><assign property="datasource:posttest.postcontent" value="value=testing123" /><assign property="datasource:posttest.source" value="http://tylr.us/testing/posttest.php" /></action><action datasource="posttest" datasourceevent="querycomplete"><alert message="[datasource:posttest.1.value]" /></action></actions></wire>


When I try this, it doesn't work. Now, there are a couple of failure points: I could have my WIRE straight-up wrong. I'm assuming that WIRE can display the output of PHP pretending to be JSON, but that assumption may be wrong. But the extremely likely situation is that I don't completely understand datasources and/or POST.

If I had to guess, I would think I might need a header, but I have no idea where to start (beyond the aforementioned Parse example from John, but I don't know where Parse-specific things stop and POST/HTTP standards begin). Any tips? :)
You will need an httpheader tag.

<httpheader name="php" Content-Type="application/json" Accept="text/plain"/> 

Then you also need to add httpheader="php" to your datasource. Since you don't have an API key, this should be all you need to do.
+1


Perfect. I had to make a couple changes (Content-Type and one change to the PHP to make my JSON easier for WIRE to read) but it works slick now. Thanks, Ian!

For those playing along at home, here's my WIRE:
<wire><datasources><datasource name="posttest" source="" query="//data" httpmethod="post" providertype="json" httpheader="php" /><httpheader name="php" Content-Type="application/x-www-form-urlencoded"/></datasources><main><panel name="bg" width="100%" height="100%" background="ff0000" onclickup="posttest"></panel></main><actions><action name="posttest"><assign property="datasource:posttest.postContent" value="value=testing123" /><assign property="datasource:posttest.source" value="http://tylr.us/testing/posttest.php" /></action><action name="return" datasource="posttest" datasourceevent="querycomplete"><alert message="[datasource:posttest.1.key]" /></action></actions></wire>

and here's my PHP:
<?php
header('Content-Type: application/json');
$data = array("key" => $_POST["value"]);
$array['data'] = $data;
echo json_encode($array);
?>