alessandrobelli's avatar

Api call from Flutter to Laravel backend return 302 - Found and redirect to web login

I'm doing a API call with bearer token from Flutter mobile app to Laravel backend. After doing a composer update I have this behaviour that unfortunately happens casually, so I don't even know if the composer update was the problem! Laravel code in the backend:

public function entriesByCase(Cases $case)
    {
        return EntryResource::collection($case->entries->sortByDesc(self::BEGIN));
    }

use App\Media;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;

class Entry extends JsonResource
{
    /**
     * Transform the resource into an array.
     * @param Request $request
     * @return array
     */
    public function toArray($request)
    {
        if (is_null($request)) {
            return [];
        }
        
        {
            return [
            'id' => $this->id,
            'begin' => $this->begin,
            'end' => $this->end,
            "inputs" => $this->inputs,
            "content" => $this->content,
            "comment" => $this->comment,
            "case_id" => $this->case_id,
            "media_id" => $this->media_id,
            "media_name" => Media::where('id', $this->media_id)->first()->name,
            "place_id" => $this->place_id,
            "communication_partner_id" => $this->communication_partner_id,
            'created_at' => $this->created_at,
            'updated_at' => $this->updated_at,
        ];
        }
    }
}

From Valet:

HTTP Requests
-------------

GET  /login                    200 OK
GET  /api/v1/entry/424         302 Found
POST /api/login                200 OK

From Telescope:

{
"x-original-host": "4937-31-19-193-140.eu.ngrok.io",
"x-forwarded-proto": "https",
"x-forwarded-for": "31.19.193.140",
"content-type": "application/json",
"authorization": "********",
"accept-encoding": "gzip",
"accept-charset": "application/json",
"user-agent": "Dart/2.15 (dart:io)",
"host": "4937-31-19-193-140.eu.ngrok.io",
"content-length": "",
"x-forwarded-host": "4937-31-19-193-140.eu.ngrok.io"
}

And for what it counts, this is the Flutter header code:

  final token = prefs.getString('token') ?? 0;
  final _headers = {
    HttpHeaders.authorizationHeader: "Bearer $token",
    HttpHeaders.acceptCharsetHeader: "application/json",
    'Content-Type': 'application/json'
  };

I absolutely don't know why this happens, because it looks it's completely casual🤷

0 likes
2 replies
rodrigo.pedra's avatar

Possibilities:

  1. In your method you call an EntryResource class, but the definition the class is called Entry, maybe it is erroring out?
  2. Is your API endpoints on the Laravel side configured to use a token based authentication? Asking this as it seems to redirect to /login, and if it is using the auth:web guard it will fail as there is no session info sent from flutter

Also can you make a request using curl or Postman to test the endpoint out?

me108's avatar

I know this is old, but I came here by Google search because I hit the same issue.

In my case, the problem was that the request validation of Laravel failed and - for a still unknown reason - returns 302 instead of 422 when called from Flutter.

When I tested the same request with the same (wrong) parameters and header configuration (application/json) in Postman, I received 422 as expected.

Hope this helps anybody who finds this.

1 like

Please or to participate in this conversation.