How do I make an http request using cookies on flutter?

后端 未结 4 938
花落未央
花落未央 2020-12-09 03:11

I\'d like to make an http request to a remote server while properly handling cookies (eg. storing cookies sent by the server, and sending those cookies when I make subsequen

4条回答
  •  旧巷少年郎
    2020-12-09 03:18

    I've published a small flutter library called requests to assist with cookie-aware http requests.

    dependencies:
      requests: ^3.0.1
    

    Usage:

    import 'package:requests/requests.dart';
    
    // ...
    
    // this will persist cookies
    var r1 = await Requests.post("https://example.com/api/v1/login", json: {"username":"...", "password":"..."} ); 
    r1.raiseForStatus();
    
    // this will re-use the persisted cookies
    var r2 = await Requests.get("https://example.com/api/v1/stuff"); 
    r2.raiseForStatus();
    print(r2.json()['id'])
    

    find out more about requests

提交回复
热议问题