سؤال

There is a difference between a PUT and POST request I send through a REST CLIENT in my API. It is implemented in CodeIgniter with Phil Sturgeon's REST server.

function station_put(){

    $data = array(
        'name' => $this->input->post('name'),
        'number' => $this->input->post('number'),
        'longitude' => $this->input->post('longitude'),
        'lat' => $this->input->post('latitude'),
        'typecode' => $this->input->post('typecode'),
        'description' => $this->input->post('description'),
        'height' => $this->input->post('height'),
        'mult' => $this->input->post('mult'),
        'exp' => $this->input->post('exp'),
        'elevation' => $this->input->post('elevation')
    );

    $id_returned = $this->station_model->add_station($data);
    $this->response(array('id'=>$id_returned,'message'=>"Successfully created."),201);


}

this request successfully inserts data into the server BUT - it renders the rest of the values NULL except for the id.

But if you change the function name into station_post, it inserts the data correctly.

Would somebody please point out why the PUT request does not work? I am using the latest version of google chrome.

Btw this API will be integrated to a BackBone handled app. Do I really need to use PUT? Or is there another workaround with the model saving function in backbone when using post?

هل كانت مفيدة؟

المحلول

Finally answered. Instead of $this->input->post or $this->input->put, it must be $this->put or $this->post because the data is not coming from a form.

نصائح أخرى

Codeigniter put_stream also failing to fetch put data, therefore I had to handle php PUT request and I found this function useful enough to save someone's time:

function parsePutRequest()
    {
        // Fetch content and determine boundary
        $raw_data = file_get_contents('php://input');
        $boundary = substr($raw_data, 0, strpos($raw_data, "\r\n"));

    // Fetch each part
    $parts = array_slice(explode($boundary, $raw_data), 1);
    $data = array();

    foreach ($parts as $part) {
        // If this is the last part, break
        if ($part == "--\r\n") break; 

        // Separate content from headers
        $part = ltrim($part, "\r\n");
        list($raw_headers, $body) = explode("\r\n\r\n", $part, 2);

        // Parse the headers list
        $raw_headers = explode("\r\n", $raw_headers);
        $headers = array();
        foreach ($raw_headers as $header) {
            list($name, $value) = explode(':', $header);
            $headers[strtolower($name)] = ltrim($value, ' '); 
        } 

        // Parse the Content-Disposition to get the field name, etc.
        if (isset($headers['content-disposition'])) {
            $filename = null;
            preg_match(
                '/^(.+); *name="([^"]+)"(; *filename="([^"]+)")?/', 
                $headers['content-disposition'], 
                $matches
            );
            list(, $type, $name) = $matches;
            isset($matches[4]) and $filename = $matches[4]; 

            // handle your fields here
            switch ($name) {
                // this is a file upload
                case 'userfile':
                    file_put_contents($filename, $body);
                    break;

                // default for all other files is to populate $data
                default: 
                    $data[$name] = substr($body, 0, strlen($body) - 2);
                    break;
            } 
        }

    }
    return $data;
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top