'Flutter 403 error http GETX and HTTP library
Hello friends I'm coding an web app. I'm using a PHP web server and HTTP library for response. But I'm getting error when I response. I print the response.statuscode in vs code and it shows me 403 error. How can I solve this problem. this is my code
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:http/http.dart' as http;
import 'package:flutter/services.dart';
import 'dart:convert';
import 'dart:typed_data';
import 'package:file_picker/file_picker.dart';
// Bu alan benim soru değişkenlerimi Getx yardımı ile tuttuğum classdır.farkllı değişken classları yazılabilir.
class activityController extends GetxController {
//5 benim değikenimin ilk değeridir.
var _activityName = "a".obs;
var _activityDetails = "a".obs;
var _activityPhoto = "a".obs;
//get ile çeker, set ile veriyi atarım.
get activityName => _activityName.value;
set activityName(yeniDeger) => _activityName.value = yeniDeger;
get activityDetails => _activityDetails.value;
set activityDetails(yeniDeger) => _activityDetails.value = yeniDeger;
get activityPhoto => _activityPhoto.value;
set activityPhoto(yeniDeger) => _activityPhoto.value = yeniDeger;
}
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
activityController _controller2 = Get.put(activityController());
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Material App',
home: Scaffold(
body: SafeArea(
child: Column(
children: [
TextField(
onChanged: (Value1) {
_controller2.activityName = Value1;
},
decoration: InputDecoration(
border: InputBorder.none, hintText: 'activityName'),
),
TextField(
onChanged: (Value1) {
_controller2.activityDetails = Value1;
},
decoration: InputDecoration(
border: InputBorder.none, hintText: 'activityDetails'),
),
ElevatedButton(
onPressed: () async {
print("aSF");
FilePickerResult? result =
await FilePicker.platform.pickFiles(
type: FileType.image,
);
if (result != null) {
Uint8List? file = result.files.first.bytes!;
final baseCodeA = base64Encode(file);
//do what you want with baseCodeA.
_controller2.activityPhoto = baseCodeA;
Clipboard.setData(
ClipboardData(text: _controller2.activityPhoto));
}
},
child: Text("FOTO EKLE")),
ElevatedButton(
onPressed: () async {
final uriAA =
'https://www.meshcurrent.online/myWebApp/add_activity.php';
var map = new Map<String, dynamic>();
map['activityName'] = _controller2.activityName;
map['activityDetails'] = _controller2.activityDetails;
map['activityPhotoBase64'] = _controller2.activityPhoto;
http.Response response = await http.post(
Uri.parse(uriAA),
body: map,
);
print(response.statusCode);
},
child: Text('ETKİNLİĞİ EKLE'),
),
],
)),
),
);
}
}
target of this code is adding some varible to database using http response
Solution 1:[1]
You must send a post request with jsonEncoded body. So you can change the parameters of post method
http.Response response = await http.post(
Uri.parse(uriAA),
body: map,
);
to
http.Response response = await http.post(
Uri.parse(uriAA),
body: json.encode(map),
);
Of course, you should import 'dart:convert' to the file.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Furkan AbbasioÄŸlu |
