我想使用dart语言将curl get请求转换为http请求,其中存在curl代码:
这是我尝试实现的代码,它没有采用所有参数:
import 'package:http/http.dart' as http;
void main() async {
var params = {
'ad_format': 'DESKTOP_FEED_STANDARD',
'access_token': '<ACCESS_TOKEN>',
};
var query = params.entries.map((p) => '${p.key}=${p.value}').join('&');
var res = await http.get('https://graph.facebook.com/v2.11/act_AD_ACCOUNT_ID/generatepreviews?$query');
if (res.statusCode != 200) throw Exception('http.get error: statusCode=${res.statusCode}');
print(res.body);
}
答案 0 :(得分:1)
您缺少用creative
添加的--data-urlencode
查询参数。此外,请避免自己使用查询参数构建url。使用Uri类。
final params = {
'ad_format': 'DESKTOP_FEED_STANDARD',
'access_token': '<ACCESS_TOKEN>',
'creative': jsonEncode({
'object_story_spec': {
// fill in the rest here..
},
}),
};
final uri = Uri.https('graph.facebook.com', '/v2.11/act_AD_ACCOUNT_ID/generatepreviews', params);
var res = await http.get(uri);
if (res.statusCode != 200) throw Exception('http.get error: statusCode=${res.statusCode}');
print(res.body);