mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
ENHANCEMENT RestfulServer now accepts form-encoded HTTP requests for PUT and POST with new FormEncodedDataFormatter class (#3792, thanks camspiers)
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@73747 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
parent
6a53df681f
commit
40910f9f71
43
api/FormEncodedDataFormatter.php
Normal file
43
api/FormEncodedDataFormatter.php
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Accepts form encoded strings and converts them
|
||||||
|
* to a valid PHP array via {@link parse_str()}.
|
||||||
|
* Use together with {@link RESTfulServer} to submit
|
||||||
|
* data via POST or PUT.
|
||||||
|
*
|
||||||
|
* Example when using cURL on commandline:
|
||||||
|
* <code>
|
||||||
|
* curl -d "Name=This is a new record" http://host/api/v1/(DataObject)
|
||||||
|
* curl -X PUT -d "Name=This is an updated record" http://host/api/v1/(DataObject)/1
|
||||||
|
* </code>
|
||||||
|
*
|
||||||
|
* @todo Format response form encoded as well - currently uses XMLDataFormatter
|
||||||
|
*
|
||||||
|
* @author Cam Spiers <camspiers at gmail dot com>
|
||||||
|
*
|
||||||
|
* @package sapphire
|
||||||
|
* @subpackage formatters
|
||||||
|
*/
|
||||||
|
class FormEncodedDataFormatter extends XMLDataFormatter {
|
||||||
|
|
||||||
|
public function supportedExtensions() {
|
||||||
|
return array(
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function supportedMimeTypes() {
|
||||||
|
return array(
|
||||||
|
'application/x-www-form-urlencoded'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function convertStringToArray($strData) {
|
||||||
|
$postArray = array();
|
||||||
|
parse_str($strData, $postArray);
|
||||||
|
return $postArray;
|
||||||
|
//TODO: It would be nice to implement this function in Convert.php
|
||||||
|
//return Convert::querystr2array($strData);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
?>
|
@ -96,13 +96,17 @@ class RestfulServerTest extends SapphireTest {
|
|||||||
$_SERVER['PHP_AUTH_PW'] = 'editor';
|
$_SERVER['PHP_AUTH_PW'] = 'editor';
|
||||||
|
|
||||||
$url = "/api/v1/RestfulServerTest_Comment/1";
|
$url = "/api/v1/RestfulServerTest_Comment/1";
|
||||||
$data = array('Comment' => 'updated');
|
$body = 'Name=Updated Comment&Comment=updated';
|
||||||
$response = Director::test($url, $data, null, 'PUT');
|
$headers = array(
|
||||||
|
'Content-Type' => 'application/x-www-form-urlencoded'
|
||||||
|
);
|
||||||
|
$response = Director::test($url, null, null, 'PUT', $body, $headers);
|
||||||
$this->assertEquals($response->getStatusCode(), 200); // Success
|
$this->assertEquals($response->getStatusCode(), 200); // Success
|
||||||
// Assumption: XML is default output
|
// Assumption: XML is default output
|
||||||
$responseArr = Convert::xml2array($response->getBody());
|
$responseArr = Convert::xml2array($response->getBody());
|
||||||
$this->assertEquals($responseArr['ID'], 1);
|
$this->assertEquals($responseArr['ID'], 1);
|
||||||
$this->assertEquals($responseArr['Comment'], 'updated');
|
$this->assertEquals($responseArr['Comment'], 'updated');
|
||||||
|
$this->assertEquals($responseArr['Name'], 'Updated Comment');
|
||||||
|
|
||||||
unset($_SERVER['PHP_AUTH_USER']);
|
unset($_SERVER['PHP_AUTH_USER']);
|
||||||
unset($_SERVER['PHP_AUTH_PW']);
|
unset($_SERVER['PHP_AUTH_PW']);
|
||||||
@ -113,13 +117,17 @@ class RestfulServerTest extends SapphireTest {
|
|||||||
$_SERVER['PHP_AUTH_PW'] = 'editor';
|
$_SERVER['PHP_AUTH_PW'] = 'editor';
|
||||||
|
|
||||||
$url = "/api/v1/RestfulServerTest_Comment";
|
$url = "/api/v1/RestfulServerTest_Comment";
|
||||||
$data = array('Comment' => 'created');
|
$body = 'Name=New Comment&Comment=created';
|
||||||
$response = Director::test($url, $data, null, 'POST');
|
$headers = array(
|
||||||
|
'Content-Type' => 'application/x-www-form-urlencoded'
|
||||||
|
);
|
||||||
|
$response = Director::test($url, null, null, 'POST', $body, $headers);
|
||||||
$this->assertEquals($response->getStatusCode(), 201); // Created
|
$this->assertEquals($response->getStatusCode(), 201); // Created
|
||||||
// Assumption: XML is default output
|
// Assumption: XML is default output
|
||||||
$responseArr = Convert::xml2array($response->getBody());
|
$responseArr = Convert::xml2array($response->getBody());
|
||||||
$this->assertEquals($responseArr['ID'], 2);
|
$this->assertEquals($responseArr['ID'], 2);
|
||||||
$this->assertEquals($responseArr['Comment'], 'created');
|
$this->assertEquals($responseArr['Comment'], 'created');
|
||||||
|
$this->assertEquals($responseArr['Name'], 'New Comment');
|
||||||
|
|
||||||
unset($_SERVER['PHP_AUTH_USER']);
|
unset($_SERVER['PHP_AUTH_USER']);
|
||||||
unset($_SERVER['PHP_AUTH_PW']);
|
unset($_SERVER['PHP_AUTH_PW']);
|
||||||
|
Loading…
Reference in New Issue
Block a user