1

I am using a REST API in Yii2 with Authorization Bearer I have configured the actionUpdate completely but somehow when using PUT to update the data, I am getting null value when I try to get the post data,

print_r($request->getBodyParam('member_id')) 

Get below result when print_r(Yii::$app->request->bodyParams):

Content -type is multipart/form-data

Array ( [------WebKitFormBoundaryAF3YWAApi5BxYUb2 Content-Disposition:_form-data;_name] => "member_id" 274505 ------WebKitFormBoundaryAF3YWAApi5BxYUb2 Content-Disposition: form-data; name="claim_type" 3 ------WebKitFormBoundaryAF3YWAApi5BxYUb2 Content-Disposition: form-data; name="visit_date" 2016-10-12 ------WebKitFormBoundaryAF3YWAApi5BxYUb2 Content-Disposition: form-data; name="provider_id" 0 ------WebKitFormBoundaryAF3YWAApi5BxYUb2 Content-Disposition: form-data; name="diagnosis" fevercc ------WebKitFormBoundaryAF3YWAApi5BxYUb2 Content-Disposition: form-data; name="reimbursement" 1 ------WebKitFormBoundaryAF3YWAApi5BxYUb2 Content-Disposition: form-data; name="non_panel" true ------WebKitFormBoundaryAF3YWAApi5BxYUb2 Content-Disposition: form-data; name="provider_name" klinik abc ------WebKitFormBoundaryAF3YWAApi5BxYUb2 Content-Disposition: form-data; name="reimbursement_reason" near to house ------WebKitFormBoundaryAF3YWAApi5BxYUb2 Content-Disposition: form-data; name="invoice_no" 1245 ------WebKitFormBoundaryAF3YWAApi5BxYUb2 Content-Disposition: form-data; name="medical_leave" 0 ------WebKitFormBoundaryAF3YWAApi5BxYUb2 Content-Disposition: form-data; name="leave_form" ------WebKitFormBoundaryAF3YWAApi5BxYUb2 Content-Disposition: form-data; name="amount_incurred" 24 ------WebKitFormBoundaryAF3YWAApi5BxYUb2-- )

Below is my actionUpdate

public function actionUpdate($id) {

    $claim = $this->findModel($id);
    $claim->scenario = 'update';

    if ( $claim->status_id == 1 ) {

        $request = Yii::$app->request;

            if (isset($request)) {

                $member_id  = $request->getBodyParam('member_id');
                $claim_type = $request->getBodyParam('claim_type');
                $visit_date = $request->getBodyParam('visit_date');
                $provider_id = $request->getBodyParam('provider_id');
                $diagnosis = $request->getBodyParam('diagnosis');
                $reimbursement = $request->getBodyParam('reimbursement');

                $userlogin_id = Yii::$app->user->identity->id;

                if ($claim->validate() ) {       
                    $claim->save();                             
                    return array('id'=>$claim->id,'msg'=>'Successfully update claim');

                } else {
                    return (ActiveForm::validate($claim));
                }
            }
    } else {
        throw new \yii\web\MethodNotAllowedHttpException('You are not allowed to update data');
    }
}
Jap Mul
  • 17,398
  • 5
  • 55
  • 66
kasmawati
  • 51
  • 2
  • 9
  • To treat a PUT/DELETE request body as form-urlencoded data, you can read php://input and parse it using parse_str. See for example http://www.lornajane.net/posts/2008/accessing-incoming-put-data-from-php. – Refilon Oct 13 '16 at 06:34
  • It's working if I use body as form-urlencoded data but I want to use multipart/form-data because I need to upload some files - @Refilon – kasmawati Oct 13 '16 at 07:22
  • I believe that uploading a file together with form data (ie multi-part) is not allowed when using PUT, only for POST. Check out the specs for HTTP. – karpy47 Nov 11 '16 at 17:23

0 Answers0