Skip to content
Advertisement

Laravel Http::post : Am I (really) sending a “bad request”?

I’m new with Laravel, not sure of this one.

I’try to retrieve this token from a FileMaker API in my Laravel app. In order to obtain it, I use a custom authentication service in my AuthServiceProvider with the Auth::viaRequest method. The request inside the method is quit simple. It’s just a basic auth POST request to the api url. The API is supposed to send me back a token.

Nevertheless, when I log() the request, I get a “bad request” error 400 and a code 960 error (probably from the FileMaker API). The 960 error returns

‘undefined’: Expected type object but found type array

I’dont get what is wrong, when I do a simple GET request in the viaRequest method it works and, when I test the POST request in Postman, it also works. I’ve compared the request’s headers in both systems (Laravel and Postman), the parameters of the request are identical…

Here is the service provider code:

<?php

namespace AppProviders;

use IlluminateFoundationSupportProvidersAuthServiceProvider as ServiceProvider;
use IlluminateSupportFacadesGate;
use IlluminateHttpRequest;
use IlluminateSupportFacadesAuth;
use IlluminateSupportFacadesHttp;
use IlluminateSupportFacadesLog;
use AppModelsUser;

class AuthServiceProvider extends ServiceProvider
{
    /**
     * The policy mappings for the application.
     *
     * @var array
     */
    protected $policies = [
        // 'AppModelsModel' => 'AppPoliciesModelPolicy',
    ];

    /**
     * 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',
            ]);
        });
    }
}

This is the log:

[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":{}}  

Thank you guys

Advertisement

Answer

Something was missing in the body. I’ve added to my request an empty json() in the body

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

Now it works. Thank you @shaedrich

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement