I have written some REST API Methods, including one for updating a DB entry:
// Update
public function update(CreateAEDRequest $request, $id) {
$aed = AED::find($id);
if(!$aed) {
return response()->json(['message' => "Dieser AED exisitiert nicht", 'code' => 404], 404);
}
$owner = $request->get('owner');
$street = $request->get('street');
$aed->owner = $owner;
$street->street = $street;
$aed->save();
return response()->json(['message' => 'Der AED wurde aktualisiert'], 200);
}
The Route is defined as:
Route::put('/aeds/{aeds}', 'APIAEDController@update');
Route::patch('/aeds/{aeds}', 'APIAEDController@update');
A Request is being handled by:
<?php
namespace App\Http\Requests;
use App\Http\Requests\Request;
use Illuminate\Http\JsonResponse;
class CreateAEDRequest extends Request
{
public function authorize()
{
// turn back to false when doing auth
return true;
}
public function rules()
{
return [
'owner' => 'required',
'street' => 'required'
];
}
}
But when I use postman and try to update the existing DB entry and I fill in the owner and street variable to be sent in POSTMAN as requested, I get the message: "Could not get any response. Returns Status Code 0"
All the other methods work fine. Is the definition of my routing not correct?
Update
When I send the data as x-www-form-urlencoded
it works! When I send them as form-data it brings up the error message.
Put is when you update the whole model and use patch when you want to update a portion or single attribute. For instance, a user may have a username they change that is stored on the user model. You would patch just the username when they change it.
PUT is a method of modifying resource where the client sends data that updates the entire resource . PATCH is a method of modifying resources where the client sends partial data that is to be updated without modifying the entire data.
When a client needs to replace an existing Resource entirely, they can use PUT. When they're doing a partial update, they can use HTTP PATCH. For instance, when updating a single field of the Resource, sending the complete Resource representation can be cumbersome and uses a lot of unnecessary bandwidth.
In Postman
POST
_method
with value PUT
or PATCH
It looks like in Postman you should point that the data you send is x-www-url-formurlencoded
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With