Structure a POST request to send a Dart object as JSON
Your grasp of HTTP semantics and Dart serialization.
Set Content-Type application/json, serialize to Map via toJson, encode with dart:convert jsonEncode, and pass the string as body.
WHAT THIS TESTS: The interviewer wants to see if you understand the boundary between Dart's type system and the HTTP wire protocol. Sending JSON is not magic; it is a contract. The question checks whether you know that a Dart object must be serialized into a format the server can understand, and whether you understand the role of headers in that contract. At the senior level, they also care if you distinguish between the http package's convenience methods and the actual HTTP semantics.
A GOOD ANSWER COVERS: A strong answer walks through four steps in order. First, state that the Content-Type header must be set to application/json so the server knows how to interpret the bytes. Second, explain that the Dart object cannot be sent directly; it must first be converted into a serializable structure, typically a Map<String, dynamic>, using a toJson method on the model class. Third, mention that you pass that Map to jsonEncode from dart:convert to produce a JSON string. Fourth, note that the resulting string is supplied as the body of the POST request, and that if you are using the http package, the headers map includes the Content-Type entry.
COMMON WRONG ANSWERS: Candidates often say they just pass the Dart object into the body parameter and let the http package handle it. This is a red flag because the http package does not automatically serialize arbitrary Dart objects. Another mistake is forgetting the Content-Type header entirely and assuming the server will infer JSON from the payload. Some candidates also confuse jsonEncode with jsonDecode, or suggest manually building a JSON string with string interpolation instead of using dart:convert. At the senior level, failing to mention UTF-8 encoding or not knowing that jsonEncode returns a string shows shallow knowledge.
LIKELY FOLLOW-UPS: The interviewer may ask how you would handle nested objects or lists inside the payload. They might ask what happens if the server returns a 415 Unsupported Media Type, which tests whether you know the header is missing or wrong. Another common follow-up is how to send form data instead of JSON, or how to add an Authorization header alongside Content-Type. You might also be asked about error handling when jsonEncode throws, or how to make the request using dart:io HttpClient instead of the http package.
ONE CONCRETE EXAMPLE: Imagine a User class with fields String name and int age. The toJson method returns a Map<String, dynamic> with keys name and age. You instantiate a User, call user.toJson to get the map, then call jsonEncode on that map to produce a string like {"name":"Ada","age":30}. Your POST call sets headers to {"Content-Type":"application/json"} and body to the encoded string. If you skip jsonEncode and pass the map directly, the http package will call toString on the map, sending something like {name: Ada, age: 30}, which is not valid JSON and will likely cause the server parser to fail.
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.