Processing flutter http request to add application / json error Can not set the body fields of a Request with content-type "application / json"

Settings "content-type" http request is sent when the flutter: "application / json" error will appear Can not set the body fields of a Request with content-type "application / json"

Request follows:

final putResponse = await http.put('http://192.168.201.21/user/modifyUser',
    body: putData,
    headers: {"token": userBasicsInfo.userTokenResult,"content-type": "application/json"}
).then((response){
  was backResult;
  if(response.statusCode == 200){
    Utf8Decoder utf8decoder = Utf8Decoder();
    backResult = json.decode(utf8decoder.convert(response.bodyBytes));
  }else{
    print ( 'data request error: $ {response.statusCode}');
  }
  return backResult;
});

  

The following error occurs after the request is sent

E/flutter ( 7295): [ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: Bad state: Cannot set the body fields of a Request with content-type "application/json".

  

Solution

Processing parameters to be submitted by jsonEncode

final putData = jsonEncode (params); // process parameters Submit
final putResponse = await http.put('http://192.168.201.21/user/modifyUser',
    body: putData,
    headers: {"token": userBasicsInfo.userTokenResult,"content-type": "application/json"}
).then((response){
  was backResult;
  if(response.statusCode == 200){
    Utf8Decoder utf8decoder = Utf8Decoder();
    backResult = json.decode(utf8decoder.convert(response.bodyBytes));
  }else{
    print ( 'data request error: $ {response.statusCode}');
  }
  return backResult;
});

  

After processing submit to success

 

note:

Body [body] setting request. It can be [String], [List] or [Map]. If it is a String, use the [encoding] encoded and used as the body of the request. Content type of request will default to "text / plain".

If [body] is List, the list will be used as the body of bytes requested.

If [body] is Map, using [encoding] encoded into the form fields. Content type is set to the request "application/x-www-form-urlencoded"this can not be covered. [encoding] default [utf8].

 

Guess you like

Origin www.cnblogs.com/gxsyj/p/11011288.html