index_post()方法中的codeigniter状态码400错误请求

时间:2017-08-08 14:31:25

标签: php rest codeigniter

在我的codeigniter restful api应用程序中执行post和put操作时,我一直收到此错误。

这是控制器:

public function index_post(){
    if(!$this->input->post('city'))
    {
        $this->response(null,400);
    }
    $id=$this->cities_model->save($this->input->post('city'));
    if(!is_null($id))
    {
        $this->response(array('response'=> $id),200);
    }
    else
    {
        $this->response(array('error'=> 'sorry, data could not be saved...'),400);
    }
}
public function index_put(){
    if(!$this->input->post('city') || !$id)
    {
        $this->response(null,400);
    }
    $update=$this->cities_model->update($id,$this->input->post('city'));
    if(!is_null($update))
    {
        $this->response(array('response' => 'content updated successfully'),200);
    }
    else
    {
        $this->response(array('error'=> 'sorry, technical error occurred, please try again later...'), 400);
    }
}

2 个答案:

答案 0 :(得分:1)

以下几乎没有改进和解决方案。

public function index_post(){

    // Use validation library, instead of checking just for value.
    $this->load->library('form_validation');
    $this->form_validation->set_rules('city','City','trim|required');

    if($this->form_validation->run() == FALSE)
    {
        // send back list of validation errors.
        $this->response($this->validation_errors(),REST_Controller::HTTP_BAD_REQUEST);
    }

    $id=$this->cities_model->save($this->post('city'));
    if(!is_null($id))
    {
        $this->response(array('response'=> $id),REST_Controller::HTTP_OK);
    }
    else
    {
        $this->response(array('error'=> 'sorry, data could not be saved...'),REST_Controller::HTTP_BAD_REQUEST);
    }
}
public function index_put(){
    // for put you need to pass id as parameter 

    // Use validation library, instead of checking just for value.
    $this->load->library('form_validation');
    $this->form_validation->set_rules('id','ID','trim|required|integer');
    $this->form_validation->set_rules('city','City','trim|required');

    if($this->form_validation->run() == FALSE)
    {
        // send back list of validation errors.
        $this->response($this->validation_errors(),REST_Controller::HTTP_BAD_REQUEST);
    }

    $update=$this->cities_model->update($this->post('id'),$this->post('city'));
    if(!is_null($update))
    {
        $this->response(array('response' => 'content updated successfully'),REST_Controller::HTTP_OK);
    }
    else
    {
        $this->response(array('error'=> 'sorry, technical error occurred, please try again later...'), REST_Controller::HTTP_BAD_REQUEST);
    }
}

现在您需要从city正文中的邮递员传递POST参数。它会调用index_post并在city正文中传递idPUT参数,然后调用index_put

答案 1 :(得分:0)

x <- "A:B:C:D:F | 1.1:2.1:3.1:4.1:6.1
A:B:D:F | 1.2:2.2:4.2:6.2
A:B:C:F | 1.3:2.3:3.3:6.3
B:C:D:F | 2.4:3.4:4.4:6.4"

data <- unlist(str_split(x, "\n"))
result <- matrix(as.numeric(NA), nrow = length(data), ncol = 6)
colnames(result) <- c("A", "B", "C", "D", "E", "F")

for (i in 1:length(data)) {
    split_data <- unlist(str_split(data[i], " [|] "))
    print(split_data)
    indices <- unlist(str_split(split_data[1], ":"))
    values <- unlist(str_split(split_data[2], ":"))

    for (j in 1:length(indices)) {
        result[i, indices[j]] <- as.numeric(values[j])
    }
}

result