【问题标题】:Laravel Http::post : Am I (really) sending a "bad request"?Laravel Http::post : 我(真的)发送“错误请求”吗?
【发布时间】:2021-09-08 11:41:12
【问题描述】:

我是 Laravel 的新手,不确定这个。

我尝试从我的 Laravel 应用程序中的 FileMaker API 检索此令牌。为了获得它,我在AuthServiceProvider 中使用Auth::viaRequest 方法自定义身份验证服务。方法内部的请求很简单。这只是对 api url 的基本身份验证 POST 请求。 API 应该给我发回一个令牌。

尽管如此,当我log() 请求时,我收到“错误请求”错误 400 和代码 960 错误(可能来自 FileMaker API)。 960错误返回

'undefined':预期的类型对象,但找到了类型数组

我不知道有什么问题,当我在 viaRequest 方法中执行一个简单的 GET 请求时它可以工作,当我在 Postman 中测试 POST 请求时,它也可以工作。我比较了两个系统(Laravel 和 Postman)中的请求头,请求的参数是相同的......

这里是服务提供者代码:


<?php

namespace App\Providers;

use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
use Illuminate\Support\Facades\Gate;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;
use App\Models\User;

class AuthServiceProvider extends ServiceProvider
{
    /**
     * The policy mappings for the application.
     *
     * @var array
     */
    protected $policies = [
        // 'App\Models\Model' => 'App\Policies\ModelPolicy',
    ];

    /**
     * Register any authentication / authorization services.
     *
     * @return void
     */
    public function boot()
    {
        $this->registerPolicies();

        Auth::viaRequest('custom-auth', function () {
            $response = Http::log()->withBasicAuth('admin', 'MyPassword')
            ->accept('application/json')
                ->post('http://MyFileMakerServer.host.ch/fmi/data/vLatest/databases/gestionrhssp-dev/sessions');
        
            if (! $response->ok()) {
                return null;
            }
            
            return new User([
                'name' => 'bla',
                'email' => 'bla',
            ]);
        });
    }
}

这是日志:

[2021-08-10 10:45:19] local.ERROR: Time 0.0095460414886475sec
Request
POST /fmi/data/vLatest/databases/gestionrhssp-dev/sessions HTTP/1.1
Content-Length: 2
User-Agent: GuzzleHttp/7
Content-Type: application/json
Authorization: Basic BASE64PASSWORDUN
Host: MyFileMakerServer.host.ch
Accept: application/json

[]
Response
HTTP/1.1 400 Bad Request
Transfer-Encoding: chunked
Content-Type: application/json
Vary: Accept-Encoding
X-Powered-By: ARR/3.0
X-Frame-Options: SAMEORIGIN
X-XSS-Protection: 1; mode=block
X-Content-Type-Options: nosniff
Date: Tue, 10 Aug 2021 10:45:19 GMT

{"messages":[{"message":"'undefined': Expected type object but found type array","code":"960"}],"response":{}}  

谢谢大家

【问题讨论】:

  • 看起来它需要请求正文中的某个对象...
  • 是的!我试过 ->withBody("{}", 'application/json') 现在它可以工作了......谢谢@shaedrich
  • 不客气。

标签: php laravel guzzle filemaker laravel-authentication


【解决方案1】:

身体里少了点什么。我在正文中添加了一个空的 json() 到我的请求中

->withBody("{}", 'application/json')

现在可以了。谢谢@shaedrich

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-24
    • 1970-01-01
    • 2017-11-28
    • 1970-01-01
    相关资源
    最近更新 更多