【问题标题】:Sending post request flutter with specific requirements发送具有特定要求的帖子请求
【发布时间】:2019-05-02 16:49:59
【问题描述】:

我需要一些关于如何使用 Flutter 中的 http 模块和一些参数发送 POST 请求的建议。

我需要将username 设置为字符串(在请求的正文中),还需要将属性设置为正文中的文件。

【问题讨论】:

标签: dart flutter httprequest


【解决方案1】:

在 Flutter 上执行请求的最简单方法是使用 Dio package

如果你的 json 有效载荷是,

{"username":"johndoe", "image":"base64 image data"}

在 dio 中的代码看起来像

import "dart:io";
import "dart:convert";
import 'package:dio/dio.dart';

// read image bytes from disk as a list
List<int> imageBytes = File("./image.png").readAsBytesSync();

// convert that list to a string & encode the as base64 files
String imageString = base64Encode(imageBytes);

// Send a post request to server
dio.post("/url-to-post-to", data: {"username":"johndoe", "image":imageString});

【讨论】:

  • 当我设置一个发布请求来注册端点时,我需要在参数中添加一个文件
  • 你的意思是上传文件?
  • 是的上传文件我的朋友
  • 希望回答你的问题?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-04-26
  • 2015-08-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多