http - 如何在flutter中使用表单数据进行http发布?

标签 http flutter dart form-data

我正在尝试执行http发布请求,并且我需要将正文指定为 form-data ,因为服务器不会将请求作为原始请求。

这就是我在做什么:

import 'dart:convert';

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

void main() => runApp(MyApp());

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {

  postTest() async {
    final uri = 'https://na57.salesforce.com/services/oauth2/token';
    var requestBody = {
      'grant_type':'password',
      'client_id':'3MVG9dZJodJWITSviqdj3EnW.LrZ81MbuGBqgIxxxdD6u7Mru2NOEs8bHFoFyNw_nVKPhlF2EzDbNYI0rphQL',
      'client_secret':'42E131F37E4E05313646E1ED1D3788D76192EBECA7486D15BDDB8408B9726B42',
      'username':'example@mail.com.us',
      'password':'ABC1234563Af88jesKxPLVirJRW8wXvj3D'
    };

    http.Response response = await http.post(
        uri,
        body: json.encode(requestBody),
    );

    print(response.body);
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Container(
        child: Center(
          child: RaisedButton(
            child: Text('Press Here'),
            onPressed: (){
              postTest();
            },
          ),
        ),
      ),
    );
  }
}

这是实际的响应:
{
    "error": "unsupported_grant_type",
    "error_description": "grant type not supported"
}

Actual response

这是预期的响应:
{
    "access_token": "00D0b000000Bb08!AR8AQO.s8mAGXCbwV77FXNLQqc2vtl8g6_16miVbgWlQMsuNHsaf2IGLUwnMVXBOfAj19iznhqhwlPOi4tagvf7FFgiJJgoi",
    "instance_url": "https://na57.salesforce.com",
    "id": "https://login.salesforce.com/id/00D0b000000Bb08EAC/0050b000005nstiAAA",
    "token_type": "Bearer",
    "issued_at": "1567993324968",
    "signature": "1+Zd/dSh9i7Moh2U0nFJLdXkVHqPlPVU6emwdYzXDPk="
}

Expected response

您可以在 postman 在原始(您获得实际响应)和表单数据(您获得预期响应)之间切换正文时进行测试

PS: header 是由客户端工具创建的临时 header 。

最佳答案

请改用Map,因为http包中的body仅具有3种类型:String,List或Map。试试这个:

final uri = 'https://na57.salesforce.com/services/oauth2/token';
var map = new Map<String, dynamic>();
map['grant_type'] = 'password';
map['client_id'] = '3MVG9dZJodJWITSviqdj3EnW.LrZ81MbuGBqgIxxxdD6u7Mru2NOEs8bHFoFyNw_nVKPhlF2EzDbNYI0rphQL';
map['client_secret'] = '42E131F37E4E05313646E1ED1D3788D76192EBECA7486D15BDDB8408B9726B42';
map['username'] = 'example@mail.com.us';
map['password'] = 'ABC1234563Af88jesKxPLVirJRW8wXvj3D';

http.Response response = await http.post(
    uri,
    body: map,
);

关于http - 如何在flutter中使用表单数据进行http发布?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57846215/

相关文章:

Flutter:CustomPainter 绘制方法被调用多次而不是一次

android - 在调试时按下 Dart DevTools 中的调试按钮之前,无法按下我的 Flutter 应用程序中的按钮

flutter design 如何创建非常复杂的只有一只康纳啮齿动物

flash - SWF 闪存文件未加载 IIS 压缩

Apache 配置 : effect of explicit :80 in http header field (host)

flutter - dart - 如何在 Quill 文本编辑器 (Flutter) 中监听文本更改

debugging - 如何在Flutter web中调试或打印?

php - 设置 cookie 和重定向而不会出现 header 错误的语义

通过 API 的 Facebook 智能好友列表

flutter - 如何在 flutter 上使用信用卡 Visa 和 Mastercard 付款?