0

i would like to transform a curl get request into http request using the dart language, the curl code is there :

enter image description here

and here is the code i have tried to implement, it doesn't take all parameters :

     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);
        }
Ramses Kouam
  • 311
  • 1
  • 8
  • 15

1 Answers1

1

You are missing the creative query parameter that you add with --data-urlencode. Furthermore, avoid building the url with query parameters yourself. Use the Uri class.

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);
dumazy
  • 13,857
  • 12
  • 66
  • 113