Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Flutter and app store releases

http.post throws Bad state when I send a Map with a JSON Content-Type

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

Short answer

In package:http a Map body means form fields, so the content type is fixed to application/x-www-form-urlencoded. Combining a Map with a Content-Type of application/json is refused with a StateError. To send JSON, turn the Map into a String with jsonEncode and set the header yourself. To send a form, drop the JSON header.

  • Applies to package:http (current docs), Dart 3.9, Flutter 3.35
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Look at the call that sends the request. You are in this error when the body argument is a Map and the headers carry a JSON content type.

    grep -rn -B2 -A8 "http.post\|http.Request(" lib/
  2. 2Match what you see, then jump to its fix

    1. 1body: {...} next to headers: {'Content-Type': 'application/json'}A Map is passed as the body of http.post while the header says JSONFix
    2. 2request.bodyFields = {...} together with request.headers['Content-Type'] = 'application/json'bodyFields is set on a Request that also carries a JSON Content-TypeFix
    3. 3The server documentation shows application/x-www-form-urlencoded or HTML form fieldsThe API wants form data, but the code sends a JSON headerFix

What does the error look like?

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

Cause 1 of 3

A Map is passed as the body of http.post while the header says JSON

1Check

The post documentation lists three body types: a String, a List<int> or a Map<String, String>. A Map is encoded as form fields, and the content type is then set to application/x-www-form-urlencoded and cannot be overridden. A String body gets text/plain by default, so for JSON you set the header yourself.

grep -rn "Content-Type" lib/

2Fix

Encode the Map to a JSON string with jsonEncode and send that String. Keep the JSON header.

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

final response = await http.post(
  Uri.https('example.com', '/api/users'),
  headers: {'Content-Type': 'application/json'},
  body: jsonEncode({'name': 'Aisha', 'age': 31}),
);

3Verify

The request is sent and you get a status code and a body back, instead of a StateError.

print(response.statusCode);
print(response.body);

Confirmed by pub.dev

Did this fix it?

Cause 2 of 3

bodyFields is set on a Request that also carries a JSON Content-Type

1Check

The bodyFields property is for form-encoded fields. The docs say that setting it on a request whose Content-Type header is anything other than application/x-www-form-urlencoded throws a StateError. Reading it in that state throws one too.

grep -rn "bodyFields" lib/

2Fix

Set the body property instead, with a JSON string, and keep the JSON header. Use bodyFields only for real form data.

final request = http.Request('POST', Uri.https('example.com', '/api/users'))
  ..headers['Content-Type'] = 'application/json'
  ..body = jsonEncode({'name': 'Aisha', 'age': 31});

final streamed = await http.Client().send(request);

3Verify

The call completes and streamed.statusCode is what the API returns.

print(streamed.statusCode);

Confirmed by pub.dev

Did this fix it?

Cause 3 of 3

The API wants form data, but the code sends a JSON header

1Check

A Map<String, String> body is exactly what a form post needs, and the package sets the right content type for it. In that case the JSON header is the mistake, not the Map.

grep -rn "headers:" lib/

2Fix

Remove the JSON Content-Type header and keep the Map of strings.

final response = await http.post(
  Uri.https('example.com', '/login'),
  body: {'email': 'aisha@example.com', 'password': 'secret'},
);

3Verify

The server accepts the form and answers with a normal status code.

print(response.statusCode);

Confirmed by pub.dev

Did this fix it?

How do I stop it coming back?

  • Decide the format first: JSON means jsonEncode and a JSON header, a form means a Map of strings and no content type.
  • Put API calls behind one small function per endpoint, so the body and the header are written together.
  • Read the package documentation for the body type before you reach for a Map.
  • Check that every value you pass to jsonEncode is a number, bool, string, null, list or map, or give it a toJson method.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.