我正在尝试从需要主体与GET请求的API获得响应。它可以与POSTMAN一起正常工作,因为它允许我们将正文添加到GET请求中。但是如何使用dart添加GET请求?
答案 0 :(得分:3)
首先,您需要在pubspec.yaml中添加此依赖项
default class WildList extends React.Component {
constructor(props){
super(props);
this.state = {
keyword: this.props.keyword
}
}
}
下面是扑朔迷离的调用API的代码段。
dependencies:
flutter:
sdk: flutter
# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
http: ^0.11.3+16
下面是从dart类中调用API。
const baseUrl = "https://jsonplaceholder.typicode.com";
class NetworkApi {
static Future getUsers() {
var url = baseUrl + "/users";
return http.get(url);
}
static Future getImages(){
return http.post('http://xyz.in/api.php',body: {
"parameter" : "value"
});
}
}
答案 1 :(得分:0)
如果从本质上说您是要添加查询参数,则可以尝试以下操作:
var body = {
'param1': 'one',
'param2': 'two',
};
var uri =
Uri.https('www.test.com', '/api/test', body);
var response = await http.get(uri, headers: {
HttpHeaders.authorizationHeader: 'Token $token',
HttpHeaders.contentTypeHeader: 'application/json',
});
有关更多信息,请参见此处:https://api.dart.dev/stable/2.0.0/dart-core/Uri/Uri.https.html