Laravel 颤振和拉威尔API。文件上传。此路由不支持GET方法。支持的方法:POST

Laravel 颤振和拉威尔API。文件上传。此路由不支持GET方法。支持的方法:POST,laravel,api,flutter,laravel-6.2,Laravel,Api,Flutter,Laravel 6.2,我想上传来自Flitter的图像。但我得到了这个错误: 此路由不支持GET方法。支持的方法: 邮局 但我将api路由设置为POSTmethod。我还发送了POSTmethod请求,但仍然收到了这个错误 但还有一件事,它对邮递员和失眠有效。没问题 我使用此标题: Content-Type: multipart/form-data Authorization: .... 请帮帮我 我的路线是: Route::post('/avatar/update', 'Api\ProfileControlle

我想上传来自Flitter的图像。但我得到了这个错误:

此路由不支持GET方法。支持的方法: 邮局

但我将api路由设置为
POST
method。我还发送了
POST
method请求,但仍然收到了这个错误

但还有一件事,它对
邮递员
失眠
有效。没问题

我使用此
标题

Content-Type: multipart/form-data
Authorization: ....
请帮帮我

我的路线是:

Route::post('/avatar/update', 'Api\ProfileController@avatar_update')->name('api.avatar.update');
我的控制器:

public function avatar_update(Request $request){
        $request->validate(array(
            'avatar' => 'required|image',
        ));
        try{
            $image = Image::make($request->avatar)->fit(250, 250);
            $photo_name = Auth::user()->username."-".strtotime(now()).'.'.$request->avatar->extension();
            $path='images/avatars/'.$photo_name;
            $image->save($path);

            if(File::exists(Auth::user()->avatar)) {
                File::delete(Auth::user()->avatar);
            }

            Auth::user()->update([
                'avatar' => 'http://attendance.isadma.com/'.$path,
            ]);
            return response()->json([
                'status' => 1,
                'message' => 'Picture updated.',
                'image' => Auth::user()->avatar
            ], 200);
        }
        catch (\Exception $e){
            return response()->json([
                'status' => 0,
                'message' => $e->getMessage()
            ], 500);
        }
    }
颤振请求代码:

@override
  Future<String> uploadProfilePic(File profilePic, String token) async {
    var postUri = Uri.parse("$url/avatar/update");
    print(profilePic.path);
    print(postUri);
    var request = http.MultipartRequest("POST", postUri);
    request.headers['authorization'] = "Bearer $token";
    request.headers['Content-Type'] = "multipart/form-data";

    request.files.add(
      await http.MultipartFile.fromPath(
        'avatar',
        profilePic.path,
        contentType: MediaType('image', 'jpg'),
        filename: basename(profilePic.path),
      ),
    );

    print(request.headers);

    request.send().then((res) async {
      print(res.headers);
      print(res.statusCode);
      print(await res.stream.bytesToString());
    }).catchError((e) {
      print(e);
    });
  }
@覆盖
将来的上载配置文件pic(文件配置文件pic,字符串标记)异步{
var postori=Uri.parse($url/avatar/update”);
打印(profilePic.path);
印刷品(postUri);
var-request=http.MultipartRequest(“POST”,postori);
request.headers['authorization']=“Bearer$token”;
request.headers['Content-Type']=“多部分/表单数据”;
request.files.add(
等待http.MultipartFile.fromPath(
"阿凡达",,
如图所示:,
contentType:MediaType('image','jpg'),
文件名:basename(profilePic.path),
),
);
打印(请求标题);
request.send()。然后((res)异步{
打印(标题);
打印(res.statusCode);
打印(wait res.stream.bytesToString());
}).catchError((e){
印刷品(e);
});
}

确保您在post请求中发送了csrf数据(_令牌)

我从api路由中删除了所有csrf验证
受保护$except=['api/*']您没有提到在哪个路由/URL上出现错误。我猜-你的帖子很好,达到了你期望的方法,但是你的验证失败了。当验证失败时,Laravel会重定向回您所在的页面。如果您没有该页面的GET路由,则会出现此错误。