【问题标题】:flutter post request with header and body带有标题和正文的颤动发布请求
【发布时间】:2019-12-18 07:19:44
【问题描述】:

任何人都可以帮助我为以下标题和正文编写颤振请求帖子,并在下面提供相应的响应;

发布请求示例

标头请求:

Content-Type:application/x-www-form-urlencoded

正文请求:

grant_type: client_credentials

scope: accounts

client_assertion_type: code**

client_assertion: code**

【问题讨论】:

    标签: flutter


    【解决方案1】:

    试试下面的,

    String hostname = "myhostname";// example
    String port = "1234"; //example
    String url = "https://$hostname:$port/as/token.oauth2";
    String body = "{'grant_type' : '$client_credentials' }" +
                  "'scope' : '$accounts'" +
                  //... etc etc 
                  "'Corresponding Response': { 'access_token': 'JWt CODE', 'expires_in': '86400', 'token_type': 'bearer', 'scope': 'payments' }"+
                 "}";
    var res = await http.post(Uri.encodeFull(url),
            headers: {
              "Accept": "application/json",
              "Content-Type" : "application/x-www-form-urlencoded"
            },
            body: body);
    

    【讨论】:

    • 错误:I/flutter (15151): "{\"timestamp\":\"2019-08-11T21:55:18.737+0000\",\"status\":400,\ "error\":\"Bad Request\",\"message\":\"必需的字符串参数 'grant_type' 不存在\",\"path\":\"/as/token.oauth2\"}"
    • "Required String parameter 'grant_type' is not present" 您的grant_type client_credentials 是否为空?
    • 这是我目前使用的代码; code Future getData() async { http.Response response = await http.post( Uri.encodeFull(url), headers: {'Content-Type': 'application/x-www-form-urlencoded'} , 正文: jsonEncode( {'grant_types': 'accounts'} + {'scope': 'client_credentials'} + {'client_assertion_type': 'urn%3Aiearer'} + {'client_assertion': 'eyJVwTTeh4BXg'}, ), ) ;打印(json解码(response.body));返回获取数据(); } code
    • 看来你打错了'grant_types' 'grant_type'
    • 是的,它在邮递员上工作得很好。无论如何它现在工作了,有两个问题第一个你解决了错字。其次,我在发送之前对身体进行了编辑,当我删除它时它开始工作。非常感谢您的帮助。
    猜你喜欢
    • 2017-08-31
    • 1970-01-01
    • 2019-06-23
    • 2018-12-04
    • 2018-12-11
    • 1970-01-01
    • 2021-11-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多