Laravel Http::post :我(真的)发送 "bad request" 了吗?
Laravel Http::post : Am I (really) sending a "bad request"?
我是 Laravel 的新手,不确定这个。
我尝试从我的 Laravel 应用程序中的 FileMaker API 检索此令牌。为了获得它,我在 AuthServiceProvider
中使用自定义身份验证服务和 Auth::viaRequest
方法。方法内的请求非常简单。这只是对 api url 的基本身份验证 POST 请求。 API 应该给我发回一个令牌。
然而,当我 log()
请求时,我收到“错误请求”错误 400 和代码 960 错误(可能来自 FileMaker API)。 960 错误 returns
'undefined': Expected type object but found type array
我不明白哪里出了问题,当我在 viaRequest
方法中执行一个简单的 GET 请求时它有效,当我在 Postman 中测试 POST 请求时,它也有效。我在两个系统(Laravel和Postman)中比较了请求的headers,请求的参数是相同的...
这是服务提供商代码:
<?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":{}}
谢谢大家
body 中缺少某些内容。我在 body
中添加了一个空的 json()
->withBody("{}", 'application/json')
现在可以了。谢谢@shaedrich
我是 Laravel 的新手,不确定这个。
我尝试从我的 Laravel 应用程序中的 FileMaker API 检索此令牌。为了获得它,我在 AuthServiceProvider
中使用自定义身份验证服务和 Auth::viaRequest
方法。方法内的请求非常简单。这只是对 api url 的基本身份验证 POST 请求。 API 应该给我发回一个令牌。
然而,当我 log()
请求时,我收到“错误请求”错误 400 和代码 960 错误(可能来自 FileMaker API)。 960 错误 returns
'undefined': Expected type object but found type array
我不明白哪里出了问题,当我在 viaRequest
方法中执行一个简单的 GET 请求时它有效,当我在 Postman 中测试 POST 请求时,它也有效。我在两个系统(Laravel和Postman)中比较了请求的headers,请求的参数是相同的...
这是服务提供商代码:
<?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":{}}
谢谢大家
body 中缺少某些内容。我在 body
中添加了一个空的 json()->withBody("{}", 'application/json')
现在可以了。谢谢@shaedrich