Flutter 如何在flatterhttp请求中添加body?
我是新来的。我正在尝试使用API 我在谷歌上搜索并找到了相关代码,但我无法在API请求中添加表单数据Flutter 如何在flatterhttp请求中添加body?,flutter,flutter-http,Flutter,Flutter Http,我是新来的。我正在尝试使用API 我在谷歌上搜索并找到了相关代码,但我无法在API请求中添加表单数据 import 'dart:convert'; import '../models/bill.dart'; Future<Stream<Bill>> getBills() async { final String url = 'https://api.punkapi.com/v2/beers'; final client = new http.Client();
import 'dart:convert';
import '../models/bill.dart';
Future<Stream<Bill>> getBills() async {
final String url = 'https://api.punkapi.com/v2/beers';
final client = new http.Client();
final streamedRest = await client.send(http.Request('post', Uri.parse(url)));
return streamedRest.stream
.transform(utf8.decoder)
.transform(json.decoder)
.expand((data) => (data as List))
.map((data) => Bill.fromJSON(data));
}
导入'dart:convert';
导入“../models/bill.dart”;
Future getBills()异步{
最终字符串url=https://api.punkapi.com/v2/beers';
final client=new http.client();
final streamdrest=wait client.send(http.Request('post',Uri.parse(url));
返回流
.变换(utf8.解码器)
.transform(json.decoder)
.展开((数据)=>(数据作为列表))
.map((数据)=>Bill.fromJSON(数据));
}
我必须在正文中通过'mobileno'=1234567890
。请帮我检查一下这个例子
Future<Album> createAlbum(String title) async {
final http.Response response = await http.post(
'https://jsonplaceholder.typicode.com/albums',
headers: <String, String>{
'Content-Type': 'application/json; charset=UTF-8',
},
body: jsonEncode(<String, String>{
'title': title,
}),
);
if (response.statusCode == 201) {
// If the server did return a 201 CREATED response,
// then parse the JSON.
return Album.fromJson(json.decode(response.body));
} else {
// If the server did not return a 201 CREATED response,
// then throw an exception.
throw Exception('Failed to load album');
}
}
未来
您也可以使用Dio软件包