邮递员的 PUT/PATCH 请求在 Laravel 中返​​回状态代码 0

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30775428/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 11:45:49  来源:igfitidea点击:

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

phplaravelpostman

提问by sesc360

I have written some REST API Methods, including one for updating a DB entry:

我编写了一些 REST API 方法,包括用于更新数据库条目的方法:

// 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"

但是,当我使用邮递员并尝试更新现有的数据库条目并按照要求填写要在邮递员中发送的所有者和街道变量时,我收到消息:“无法获得任何响应。返回状态代码 0”

enter image description here

在此处输入图片说明

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

所有其他方法都可以正常工作。我的路由定义不正确吗?

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

更新当我发送数据x-www-form-urlencoded时!当我将它们作为表单数据发送时,它会显示错误消息。

回答by Oleg Abrazhaev

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

看起来在 Postman 中,您应该指出您发送的数据是x-www-url-formurlencoded.

enter image description here

在此处输入图片说明

回答by Kate S.

In Postman

Postman

  1. Change method to POST
  2. Add new variable _methodwith value PUTor PATCH
  1. 将方法更改为 POST
  2. 添加_method具有值PUT或的新变量PATCH