错误状态:无法设置内容类型为"application/json"的请求的正文字段 [英] Bad state: Cannot set the body fields of a Request with content-type "application/json"

查看:43
本文介绍了错误状态:无法设置内容类型为"application/json"的请求的正文字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Map<String,String> headers = {'Content-Type':'application/json','authorization':'Basic c3R1ZHlkb3RlOnN0dWR5ZG90ZTEyMw=='};

var response = await post(Urls.getToken,
        headers: headers,
        body: {"grant_type":"password","username":"******","password":"*****","scope":"offline_access"},
      );

执行此操作时,我无法接收数据,并且抛出的错误是

When I execute this I am unable to recieve data and the error thrown is

状态错误:无法设置内容类型为"application/json"的请求的正文字段

Bad state: Cannot set the body fields of a Request with content-type "application/json"

推荐答案

您需要将主体包裹在 jsonEncode 中.

You need to wrap the body in jsonEncode.

import 'package:http/http.dart' as http;
import 'dart:convert';

Map<String,String> headers = {'Content-Type':'application/json','authorization':'Basic c3R1ZHlkb3RlOnN0dWR5ZG90ZTEyMw=='};
final msg = jsonEncode({"grant_type":"password","username":"******","password":"*****","scope":"offline_access"});

var response = await post(Urls.getToken,
               headers: headers,
               body: msg,
            );

这篇关于错误状态:无法设置内容类型为"application/json"的请求的正文字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆