Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get a PUT request with Codeigniter

I have a problem right now with CodeIgniter : I use the REST Controller library (which is really awesome) to create an API but I can not get PUT requests...

This is my code :

function user_put() {
    $user_id = $this->get("id");
    echo $user_id;
    $username = $this->put("username");
    echo $username;
}

I use curl to make the request :

curl -i -X PUT -d "username=test" http://[...]/user/id/1

The user_id is full but the username variable is empty. Yet it works with the verbs POST and GET. Have you any idea please?

Thank you !

like image 441
ncrocfer Avatar asked Apr 04 '11 15:04

ncrocfer


3 Answers

According to: http://net.tutsplus.com/tutorials/php/working-with-restful-services-in-codeigniter-2/ we should consult https://github.com/philsturgeon/codeigniter-restserver/blob/master/application/libraries/REST_Controller.php#L544 to see that this method:

/**
 * Detect method
 *
 * Detect which method (POST, PUT, GET, DELETE) is being used
 * 
 * @return string 
 */
protected function _detect_method() {
  $method = strtolower($this->input->server('REQUEST_METHOD'));

  if ($this->config->item('enable_emulate_request')) {
    if ($this->input->post('_method')) {
      $method = strtolower($this->input->post('_method'));
    } else if ($this->input->server('HTTP_X_HTTP_METHOD_OVERRIDE')) {
      $method = strtolower($this->input->server('HTTP_X_HTTP_METHOD_OVERRIDE'));
    }      
  }

  if (in_array($method, array('get', 'delete', 'post', 'put'))) {
    return $method;
  }

  return 'get';
}

looks to see if we've defined the HTTP header HTTP_X_HTTP_METHOD_OVERRIDE and it uses that in favor of the actual verb we've implemented on the web. To use this in a request you would specify the header X-HTTP-Method-Override: method (so X-HTTP-Method-Override: put) to generate a custom method override. Sometimes the framework expects X-HTTP-Method instead of X-HTTP-Method-Override so this varies by framework.

If you were doing such a request via jQuery, you would integrate this chunk into your ajax request:

beforeSend: function (XMLHttpRequest) {
   //Specify the HTTP method DELETE to perform a delete operation.
   XMLHttpRequest.setRequestHeader("X-HTTP-Method-Override", "DELETE");
}
like image 180
jcolebrand Avatar answered Nov 05 '22 10:11

jcolebrand


You can try to detect the method type first and seperate the different cases. If your controller only handles REST functions it could be helpful to put get the required information in the constructor.

switch($_SERVER['REQUEST_METHOD']){
   case 'GET':
      $var_array=$this->input->get();
      ...
      break;
   case 'POST':
      $var_array=$this->input->post();
      ...
      break;
   case 'PUT':
   case 'DELETE':
      parse_str(file_get_contents("php://input"),$var_array);
      ...
      break;
   default:
      echo "I don't know how to handle this request.";
}
like image 29
Johnny2k Avatar answered Nov 05 '22 12:11

Johnny2k


In CodeIgniter 4 use getRawInput which will retrieve data and convert it to an array.

$data = $request->getRawInput();
like image 2
Sabeer Avatar answered Nov 05 '22 11:11

Sabeer