无法获取包含表单数据的HTTP响应

huangapple go评论58阅读模式
英文:

Fail to get http response with form-data

问题

无法从Flutter中的HTTP POST请求中获取响应,但URL和数据已在Postman中验证。

var map = new Map<String, String>();
final url = Uri.parse(globals.ServerDomain + '/login');
Map<String, String> requestBody = <String, String>{
  'username': '80889099',
  'password': '123456789abcde'
};
var request = http.MultipartRequest('POST', url)
  ..fields.addAll(requestBody);
var response = await request.send();
final respStr = await response.stream.bytesToString();
print(respStr);

没有返回结果。

英文:

I am not able to get response from http POST in Flutter but URL and data are verified in Postman.

无法获取包含表单数据的HTTP响应

    var map = new Map&lt;String, String&gt;();
    final url = Uri.parse(globals.ServerDomain + &#39;/login&#39;);
    Map&lt;String, String&gt; requestBody = &lt;String, String&gt;{
      &#39;username&#39;: &#39;80889099&#39;,
      &#39;password&#39;: &#39;123456789abcde&#39;
    };
    var request = http.MultipartRequest(&#39;POST&#39;, url)
      ..fields.addAll(requestBody);
    var response = await request.send();
    final respStr = await response.stream.bytesToString();
    print(respStr);

no result returned.

答案1

得分: 2

Flutter有一个很棒的包叫做Dio,用来处理各种HTTP请求。使用它非常容易实现你想要的功能,如果你正在使用表单数据,那么这就是你应该使用的方法。要了解更多详情,请查看这个链接:https://pub.dev/packages/dio#sending-formdata

示例代码:

final formData = FormData.fromMap(
{'username': '80889099',
  'password': "123456789abcde",
});
final response = await dio.post('${globals.ServerDomain}/login', data: formData);
英文:

Flutter has this great package called Dio to handle all sort of http requests. It's very easy to do what you want with it, you are using form data so this is what you should use. For more details check this https://pub.dev/packages/dio#sending-formdata

Example code:

final formData = FormData.fromMap(
{&#39;username&#39;: &#39;80889099&#39;,
  &#39;password&#39;: &quot;123456789abcde&quot;,
});
final response = await dio.post(&#39;${globals.ServerDomain}/login&#39;, data: formData);

答案2

得分: 0

看起来这是一个大型项目,更改包可能不是一个好选择,尝试添加请求头

'Content-Type': 'multipart/form-data'

示例代码:

Map<String, String> headers = <String, String>{
     'Authorization': 'Basic ${base64Encode(utf8.encode('user:password'))}',//your any other header
     'Content-Type': 'multipart/form-data'
  };

var map = new Map<String, String>();
final url = Uri.parse(globals.ServerDomain + '/login');
Map<String, String> requestBody = <String, String>{
  'username': '80889099',
  'password': '123456789abcde'
};
var request = http.MultipartRequest('POST', URL)
  ..headers.addAll(headers)
  ..fields.addAll(requestBody);
var response = await request.send();
final respStr = await response.stream.bytesToString();
print(respStr);

此外,请检查是否已授予互联网权限(这可能不是问题的原因,只是提供信息)。

英文:

Looks like Its big project changing the package is not a good option try adding request header

&#39;Content-Type&#39;: &#39;multipart/form-data&#39;

example code :

Map&lt;String, String&gt; headers= &lt;String,String&gt;{
     &#39;Authorization&#39;:&#39;Basic ${base64Encode(utf8.encode(&#39;user:password&#39;))}&#39;,//your any other header
     &#39;Content-Type&#39;: &#39;multipart/form-data&#39;
  };

var map = new Map&lt;String, String&gt;();
    final url = Uri.parse(globals.ServerDomain + &#39;/login&#39;);
    Map&lt;String, String&gt; requestBody = &lt;String, String&gt;{
      &#39;username&#39;: &#39;80889099&#39;,
      &#39;password&#39;: &#39;123456789abcde&#39;
    };
    var request = http.MultipartRequest(&#39;POST&#39;, URL)
      ..headers.addAll(headers)
      ..fields.addAll(requestBody);
    var response = await request.send();
    final respStr = await response.stream.bytesToString();
    print(respStr);

Additionally check if internet permission is given (may not be the cause just info)

huangapple
  • 本文由 发表于 2023年2月16日 12:45:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/75467949.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定