i want to get data from my api web server. I tested on Postman the data is there, even when I try it on my browser the data is still there. but when i want to use the api in my flutter app why is the api empty.
my code to get api.
class DioClient {
final Dio _dio = Dio(
BaseOptions(
baseUrl: 'https://spotify.test/api',
contentType: "application/json",
responseType: ResponseType.json,
)
);
Future<HeaderList> getDatas() async {
Response datas = await _dio.get('/load/songs');
print('data = ${datas.data}');
return HeaderList.fromJson(datas.data);
}
}
my model
class HeaderList {
final int total;
final List<Artist> artist;
final List<Song> songs;
HeaderList({
required this.total,
required this.songs,
required this.artist,
});
factory HeaderList.fromJson(Map<String, dynamic> json) => HeaderList(
total: json['total data'],
artist: List<Artist>.from(json['artist'].map((element) => Artist.fromJson(element))),
songs: List<Song>.from(json['songs'].map((element) => Song.fromJson(element))),
);
}
is there any error in my code or my laravel web project ?. because I'm still a beginner in laravel ?
CodePudding user response:
You shouldn't need Dio for a simple request like this; just use package:http
.
There are a couple of problems with your code. In Postman you are using the POST verb, but GET in Dart. You are also telling the server you are sending JSON, but it want (see your Postman request) form data.
Try this:
import 'package:http/http.dart' as http;
void main() async {
final r = await http.post(
Uri.https('spotify.test', 'api/load/songs'),
body: {
'password': 'open123456',
'password_confirmation': 'open123456',
},
);
print(r.statusCode);
print(r.body);
}