Home > Enterprise >  Passing data using HTTP PUT method works only when using query params and not from body
Passing data using HTTP PUT method works only when using query params and not from body

Time:11-01

Basically what my controller does is update the data from database. However when testing the api for the controller, the data passed through query parameters works while data passed from body doesn't.

For eg: When passing data from query parameters When passing data from query parameters

vs When passing data from body When passing data from body

My controller looks like this

public function update(Request $request, $id)
    {
        if (Contact::where('id', $id)->exists()) {
            $editedContactData = Contact::find($id);
            $editedContactData->province =  is_null($request->province) ? $editedContactData->province : $request->province;
            $editedContactData->district = is_null($request->district) ? $editedContactData->district : $request->district;
            $editedContactData->local = is_null($request->local) ? $editedContactData->local : $request->local;
            $editedContactData->spokesman =  is_null($request->spokesman) ? $editedContactData->spokesman : $request->spokesman;
            $editedContactData->phone = is_null($request->phone) ? $editedContactData->phone : $request->phone;
            $editedContactData->email = is_null($request->email) ? $editedContactData->email : $request->email;

            $editedContactData->save();
            
            return response()->json([
                "message" => "Contact Updated successfully",
                "editedContactData" => $editedContactData
            ], 201);
        }else{
            return response()->json([
                "message" => "Contact Not Found."
            ], 404);
        }
    }

I think there is problem with my controller, yet I'm unable to find the solutions. Any problem the code might have?

For anybody who want to see headers passed headers

CodePudding user response:

You can use post() to get body parameters

$datarequest = $this->input->post(); // this will contain all body parameters

CodePudding user response:

enter image description here

try to send put request like this, it may works.

  • Related