英文:
Flutter Dio dio.post is always returning 419 bad response even when I Log other status codes, including 200 on a Laravel back-end
问题
I'm building this multi-platform front-end using flutter for my webpage and I get an error when connecting to my Laravel back-end.
我正在使用Flutter构建多平台前端,连接到我的Laravel后端时出现错误。
I'm tracing the issue to the request on flutter, which I'm using Dio ^5.1, since I can log in 200 status code on my Laravel back-end, so all I'm missing is to confirm 200 on flutter and proceed with login operation.
我正在追踪问题到Flutter上的请求,我正在使用Dio ^5.1,因为我可以在我的Laravel后端记录200状态代码,所以我唯一缺少的是在Flutter上确认200并继续登录操作。
My login_controller.dart is
我的login_controller.dart是
void submitLogin() async {
try {
bool isValid = await validateEmail(inputUser.text);
if (isValid) {
await api.call(
{
"email": inputUser.text,
"password": inputPass.text,
},
endpoint: '/login',
method: 'POST',
);
} else {
showSnackbar(msg: "Algo errado, favor validar os dados inseridos.", type: 0);
}
} catch (e) {
showSnackbar(msg: e.toString(), type: 0);
Get.back();
}
}
Nothing wrong in here as far as I'm aware,
在我看来,这里没有任何问题,
my api.call function is where lies the problem:
我的api.call函数是问题所在:
print(postResponse) can never be reached and I'm getting 419 bad request on catch DioError everytime, even with successful laravel login.
print(postResponse)永远无法到达,即使在Laravel登录成功的情况下,每次都会捕获DioError并得到419坏请求。
Future<Response?> call(Map<String, dynamic> data, {required String endpoint, required String method}) async {
try {
if (token.value == "") {
await getToken();
}
options.headers.addIf(token.value != "", 'token', token.value);
if (method == "GET") {
Response getResponse = await dio.get(endpoint);
print(getResponse.statusCode);
return getResponse;
}
if (method == "POST") {
Response postResponse = await dio.post(endpoint, data: jsonEncode(data.values));
print(postResponse.statusCode);
return postResponse;
}
} on DioError catch (e) {
print(e.response);
showSnackbar(msg: e.toString(), type: 0);
}
}
Is this enough for the question? Did I miss something? Thanks in advance.
这足够回答您的问题了吗?我有漏掉什么吗?提前感谢您。
I'm running local laravel app with --host = IPv4 address
and my BaseUrl on Flutter api is IPv4:8000
did this to be sure there is no error related to addresses like 127.0.0.1 and 10.0.0.2, but still Dio returns 419 bad request..
I can also log in successfully with POSTMAN.
我正在使用--host = IPv4地址运行本地Laravel应用程序,我的Flutter API的BaseUrl是IPv4:8000
我这样做是为了确保没有与127.0.0.1和10.0.0.2之类的地址相关的错误,但Dio仍然返回419坏请求。。
我还可以在POSTMAN中成功登录。
英文:
I'm building this multi-platform front-end using flutter for my webpage and I get an error when connecting to my Laravel back-end.
I'm tracing the issue to the request on flutter, which I'm using Dio ^5.1, since I can log in 200 status code on my Laravel back-end, so all I'm missing is to confirm 200 on flutter and proceed with login operation.
My login_controller.dart is
void submitLogin() async {
try {
bool isValid = await validateEmail(inputUser.text);
if (isValid) {
await api.call(
{
"email": inputUser.text,
"password": inputPass.text,
},
endpoint: '/login',
method: 'POST',
);
} else {
showSnackbar(msg: "Algo errado, favor validar os dados inseridos.", type: 0);
}
} catch (e) {
showSnackbar(msg: e.toString(), type: 0);
Get.back();
}
}
Nothing wrong in here as far as I'm aware,
my api.call function is where lies the problem:
print(postResponse) can never be reached and I'm getting 419 bad request on catch DioError everytime, even with successful laravel login.
Future<Response?> call(Map<String, dynamic> data, {required String endpoint, required String method}) async {
try {
if (token.value == "") {
await getToken();
}
options.headers.addIf(token.value != "", 'token', token.value);
if (method == "GET") {
Response getResponse = await dio.get(endpoint);
print(getResponse.statusCode);
return getResponse;
}
if (method == "POST") {
Response postResponse = await dio.post(endpoint, data: jsonEncode(data.values));
print(postResponse.statusCode);
return postResponse;
}
} on DioError catch (e) {
print(e.response);
showSnackbar(msg: e.toString(), type: 0);
}
}
Is this enough for the question? Did I miss something? Thanks in advance.
I'm running local laravel app with --host = IPv4 address
and my BaseUrl on Flutter api is IPv4:8000
did this to be sure there is no error related to addresses like 127.0.0.1 and 10.0.0.2, but still Dio returns 419 bad request..
I can also log in successfully with POSTMAN.
答案1
得分: 1
从Laravel中提取路由时,请将它们放入api文件中。 419错误是因为您没有发送csrf令牌。 如果您已经在api.php路由上,请从app>providers>routeserviceprovider文件中删除web中间件,它将起作用+1 如果您这样做我会很感激
英文:
When extracting routes from Laravel, put them in the api file. 419 error occurs because you do not send csrf tokens. If you are already on api.php route, remove the web middleware from the app>providers>routeserviceprovider file, it will work +1 I would appreciate if you do
答案2
得分: 0
I managed to work around my issue, as I'm using sanctum authentication in Laravel, it was yielding an error on Http\Controllers\Auth\AuthenticatedSessionController.php
So I reworked my store function to return custom status:
public function store(LoginRequest $request): Response
{
try{
$request->authenticate();
return response(['status' => 'success']);
} catch(e){
return response(['status' => 'failure']);
}
}
this way I can work with my postResponse in flutter, reminding that both will yield a 200 http status code, so I'll rely on text return 'status' to check for authentication status instead.
英文:
Ok, I managed to work around my issue, as I'm using sanctum authentication in Laravel, it was yielding an error on Http\Controllers\Auth\AuthenticatedSessionController.php
So I reworked my store function to return custom status `
public function store(LoginRequest $request): Response
{
try{
$request->authenticate();
return response(['status' => 'success']);
} catch(e){
return response(['status' => 'failure']);
}
}`
this way I can work with my postResponse in flutter, reminding that both will yield a 200 http status code, so I'll rely on text return 'status' to check for authentication status instead
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论