tags:

views:

1624

answers:

6

Hello,

I'm currently writing a REST service using CakePHP and I was wondering how can I send an array of item_ids through REST to an action.

class ItemsController extends AppController {

var $components = array('RequestHandler');
var $helpers = array('Text', 'Xml');

function index()
{

}

function create($itemsArr)
{
        //handle $itemsArr here and sends data as xml to views.
}

}

I guess my real question is, How will the request look like?

http://mysite/items/create/???

I guess I can pass the values as 1 argument using implode(someSeperator, array) but is there a better solution?

Thanks in advance

A: 

This actually applies to any web page, not just CakePHP.

Any web page which wants to send a large number of fields has to include them all in their POST request.

If you had a web page form with 50 inputs and a submit at the bottom, the page would by default serialise the data and send it in the form request.

If you don't want all the data sent in seperate bars, then using a seperator would work fine too and would mean they all went in 1 parameter.

Somthing like:

http://mysite/items/create?mydata=23-45-65-32-43-54-23-23-656-77
Jon Winstanley
A: 

another option:

$safedata = base64_encode(serialize($arrayofdata));

and pass it to URL as safe string.

then uncompress it:

$data = unserialize(base64_decode($safedata);
Sergei
+1  A: 

If you truly want to be RESTful about this, you'd definitely want to use a POST request to create records. That's if you care to be strict about the standard, but it would also help you because I'm reading that your array's length could vary tremendously - sometimes 1 ID, perhaps 30 other times, etc. URI query strings do (or used to?) have a maximum character limit that you could conceivably bump up against.

If you roll with a POST request, you could easily passing in a comma-delimited list (think about how a field name with multiple checkboxes is passed) or, my favorite mechanism, a JSON-encoded array (represented as a string that can easily be JSON-decoded on the other side.

Rob Wilkerson
A: 

Hi!

When you work with Rest, the application must send and receive XMLs.

The Request for index will be like this:

domain/items/index.xml

The request for create will be like this:

domain/itens/create.xml

In the book.cakephp.org you find this: "If a POST or PUT request has an XML content-type, then the input is taken and passed to an instance of Cake's Xml object, which is assigned to the $data property of the controller."

Here an exemple of a request to your create action:

$response = $this->Http->post($url, $xml, array('header' => array('Content-Type'=>'text/xml')));

Without the 3o. parameter: array('header'=> array('Content-type'=>'text/xml')) your request won't work, because RequestHandler won't recognize your post as a valid XML

A: 

I do't kwon this!

Hong Joy
+2  A: 

This is not REST. REST is about using HTTP, not XML!

A typical HTTP REQUEST to create an item would be like this

PUT http://mysite/items/ HTTP/1.1
Host: xxxxx

<myitem>
<text> asdasdas </text>
</myitem>

And you can use whatever you want in the body of the request. XML, JSON, PHP SERIALIZE or your own data format.

toto