Search code examples
flutterhttppostgethttp-headers

how do you send a get request to get dropbox access token in flutter, I am finding it really tricky to get the get request working


I am quite new to flutter and have been struggling a lot with http post and get request, if you could please help me write the http get request to get the access token, it would be amazing. I am developing an android application, how do I get a redirect link that leads back to it?


Solution

  • A simple https call can be made through the http package. First, declare your import of the package at the top of your dart file:

    import 'package:http/http.dart' as http;
    

    Here is a general http get call:

    var apiResponse = await http.get(
      Uri.parse(<YOUR-API-LINK-STRING-HERE>),
    );
    String responseBody = apiResponse.body;
    

    This returns the response body as a string, which you can then JSON-decode to query through the objects in the response.

    Here is an example of a HTTP Post:

    var httpPost = await http.post(
       Uri.parse(<YOUR-API-LINK-STRING-HERE>),
       headers: {<MAP-OF-HEADERS-HERE>},
       body: json.encode(<String, dynamic>{<MAP-OF-BODY-HERE>}),
     );
     var jsonPostResponse = jsonDecode(httpPost);
    

    What you're describing with Dropbox and a redirect link sounds more like OAuth2. You can read more about how OAuth2 works here, and there's even a Flutter package to help with integrating it available here.