Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PUT/PATCH request with postman returns status code 0 in Laravel

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"

enter image description here

All the other methods work fine. Is the definition of my routing not correct?

Update When I send the data as x-www-form-urlencodedit works! When I send them as form-data it brings up the error message.

like image 820
sesc360 Avatar asked Jun 11 '15 08:06

sesc360


People also ask

What is difference between put and patch laravel?

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.

What is put and patch in Postman?

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.

What is Patch Vs put?

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.


2 Answers

In Postman

  1. Change method to POST
  2. Add new variable _method with value PUT or PATCH
like image 166
Kate S. Avatar answered Sep 24 '22 18:09

Kate S.


It looks like in Postman you should point that the data you send is x-www-url-formurlencoded.

enter image description here

like image 26
Oleg Abrazhaev Avatar answered Sep 24 '22 18:09

Oleg Abrazhaev