zoukankan      html  css  js  c++  java
  • CodeIgniter RestServer中put请求获取不到參数的问题解决

    近期用restserver遇到个蛋疼的问题,发现$this->put得到的參数都是null。查了一下发现。这貌似这个普遍问题,參见链接:https://github.com/chriskacerguis/codeigniter-restserver/issues/362

    还是先来看下官方的解释:參见 http://code.tutsplus.com/tutorials/working-with-restful-services-in-codeigniter-2--net-8814

    $this->put()
    
    Reads in PUT arguments set in the HTTP headers or via cURL.

    即该函数能够读取通过cURL訪问时携带的put參数,或者在http headers里的參数。

    可是经过測试,即便參数放headers里。$this->put()也訪问不到,其根本原因可能是在源代码上某个地方给屏蔽了。

    杂家临时也没找到解决的根本方法,但下面两种能够临时解决这个问题:

    1。与post保持一致,仍然在body里传參数。

    在基类里写个函数:

        public function getPut($key){
            return $this->input->input_stream($key);
        }

    client訪问的时候正常传參数在body里。就ok了。注意此时通过$this->post()是得不到參数的。必须从input_stream里取。上述函数支持多字段同一时候取,如:

    $data = $this->getPut(array('tel', 'name', 'addr'));
    事实上CI里从input出来的函数应该都支持多字段同一时候取,但Restserver的this->get() post()却不支持。

    补充:当把參数放body里时。直接用$this->put()就能够获得到相应字段了,文档说是在headers,实际是在body里!但$this->put()不支持多字段,故上述函数还是有意义的。


    $this->delete()也有这个问题,读不到headers里的參数,但能读到body里的!

    !!

    2。參数在header里传,基类里写个函数:

        /**
         * 获得key相应的header
         * @param $key
         * @return mixed
         */
        public function getHeader($key){
            return $this->input->get_request_header($key, TRUE);
        }

    个人推荐第一种哈。參数在body里传!

    能依照http规矩来最好,header里不要烂用。


    ps:restserver里put获得不到參数的问题跟Content-Type:application/json 这个设置无关。

    ------欢迎大家增加PHP CodeIgniter社区群:460132647

  • 相关阅读:
    hive on hbase
    django多表操作
    django单表操作
    django模板
    django路由初识
    python 相关模块安装 国内镜像地址
    django初识
    Python中属性和描述符的简单使用
    pip安装包(python安装gevent(win))
    jQuery 事件方法
  • 原文地址:https://www.cnblogs.com/slgkaifa/p/7147119.html
Copyright © 2011-2022 走看看