如何在Flutter GET Api请求中添加多个标头

时间:2019-02-22 08:14:45

标签: http cookies dart flutter http-headers

如何在Flutter API标头中添加set-cookie。我使用了以下dart插件:

Dart Plugin

我正在关注这些链接,但无法添加标题。

Flutter Cookbook

Stackoverflow Question

下面是我的代码:

Future<Map> getApi() async {

    Map<String, String> headers = {};

    // HEADERS TO BE ADDED
    headers['Set-Cookie'] = "user_image=; Path=/";
    headers['Set-Cookie'] = "user_id=Administrator; Path=/";
    headers['Set-Cookie'] = "system_user=yes; Path=/";
    headers['Set-Cookie'] = "full_name=Administrator; Path=/";
    headers['Set-Cookie'] = "sid=123456789; Expires=Mon, 25-Feb-2019 11:01:39 GMT; Path=/";

    // API RESPONSE
    http.Response response = await http.get(
      apiUrl,
      headers: headers,
    );

    // CONVERT TO JSON AND MAP
    Map responseBody = convert.jsonDecode(response.body);

    return responseBody;
  }

3 个答案:

答案 0 :(得分:0)

这应该做你想要的

  HttpClient client = HttpClient();
  final request = await client.getUrl(Uri.parse("http://www.example.com/"));
  request.cookies
    ..add((Cookie('user_image', '')..path = '/'))
    ..add((Cookie('user_id', 'Administrator')..path = '/'))
    ..add((Cookie('system_user', 'yes')..path = '/'))
    ..add((Cookie('full_name', 'Administrator')..path = '/'))
    ..add((Cookie('sid', '123456789')..path = '/')
      ..expires = DateTime.utc(2019, 2, 25, 11, 01, 39));
  //request.write(...)
  final response = await request.close();

答案 1 :(得分:0)

这应该有效;

    private void SelectText(int start, int length)
    {
        TextRange textRange = new TextRange(richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd);
        TextPointer pointerStart = textRange.Start.GetPositionAtOffset(start, LogicalDirection.Forward);
        TextPointer pointerEnd = textRange.Start.GetPositionAtOffset(start + length, LogicalDirection.Backward);

        richTextBox.Selection.Select(pointerStart, pointerEnd);
    }

答案 2 :(得分:0)

借助此link上给出的解决方案,我能够解决我的问题。以下是添加标头的HTTP请求:

http.Response response = await http.get(
   apiUrl,
   headers: {'Cookie': 'sid=123456789'},
);

感谢帮助人员。