简体   繁体   English

如何使用 url 编码 header 和 Flutter/Dart 中的主体制作 HTTP POST 请求

[英]How to make HTTP POST request with url encoded header and body in Flutter/Dart

I've been trying to make a post request to an endpoint that requires both the request header and body to be encoded however so far the responses I've been getting back are 400 errors.我一直在尝试向一个端点发出一个发布请求,该端点需要请求 header 和要编码的正文,但是到目前为止我得到的响应是 400 个错误。 The only way I've gotten the request to work so far is with a curl request and Thunder client in VSCode.到目前为止,我获得工作请求的唯一方法是使用 curl 请求和 VSCode 中的 Thunder 客户端。 Below is my request body.以下是我的请求正文。 Any sort of help would be appreciated thanks.任何形式的帮助将不胜感激。

With the flutter http package:使用 flutter http package:

Response response = await post(
      Uri.parse(URL), 
      headers: {
      'Authorization': 'Basic ${base64Encode(utf8.encode('$ID:$SECRET'))}',
      'Content-Type': 'application/x-www-form-urlencoded',
      }, 
      encoding: Encoding.getByName('utf-8'),
      body: {
      'grant_type': 'refresh_token',
      'refresh_token': refreshToken,
      'redirect_uri':
          redirectUri,
      }
    );

Error:错误:

在此处输入图像描述

With the flutter Dio package:使用 flutter Dio package:

Dio.Dio dio = Dio.Dio();
    Dio.Response response = await dio.post(
        URL,
        data: {
          'grant_type': 'refresh_token',
          'refresh_token': refreshToken,
          'redirect_uri':
              redirectUri,
        },
        options:
            Dio.Options(contentType: Dio.Headers.formUrlEncodedContentType, headers: <String, String>{'Authorization': 'Basic ${base64Encode(utf8.encode('$ID:$SECRET'))}'}));

Error:错误:

在此处输入图像描述

As your getting responce back from the server 400 so api is hitting server.随着您从服务器 400 得到响应,因此 api 正在访问服务器。 Try debug from backend to know exact issuse in detail.尝试从后端调试以详细了解确切的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM