RESTserver POST请求格式

时间:2011-08-11 18:04:01

标签: codeigniter rest

我正在使用CodeIgniter和RestServer for CI开发API(见下文)。我也使用Firefox RestClient插件来测试API。

我想知道的是如何做测试帖(格式)。

尝试{“desc”:“value”}但它没有用。 API没有“看到”传入的帖子字段。

http://net.tutsplus.com/tutorials/php/working-with-restful-services-in-codeigniter-2/

2 个答案:

答案 0 :(得分:2)

帖子正文不需要具有特定的格式,但最方便的是以与Web浏览器编码表单数据相同的方式对正文进行编码,特别是Content-Type: application/x-www-form-urlencoded。特别是,HostContent-Length标头不是可选,并且通常需要Content-Type标头来告诉服务器如何解释正文。一个格式良好的POST请求将如下所示:

POST /path/to/resource HTTP/1.0
Host: example.com:80
Content-Length: 21
Content-Type: application/x-www-form-urlencoded

key=value&key2=value2

服务器仍然需要识别内容类型的标题并以这种方式解析主体。

请注意,数据在所有标题之后,而不是作为请求路径的一部分(在第一行中)。

答案 1 :(得分:0)

或者,您可以使用我为CI编写的Proxy Library。有了它,您可以模拟任何可能的API调用(它也适用于流行的REST API),使用更简单的语法而不是使用cURL ......

// An example call to your API end point using POST, will be simply
$this->load->library('proxy');
$this->proxy->http('POST', 'http://somesite.com/api/users', array('username' => 'foo', 'password' => 'bar'));

您也可以定义任何HTTP标头(如API Key或其他任何内容)。

相关问题