# Dart: FormatException: Unexpected character (at character 1) <!DOCTYPE html> — jsonDecode of an HTML page

**Code:** `FormatException: Unexpected character` · **Category:** Flutter / Dart · **Seen on:** Flutter

`jsonDecode` received something that is not JSON, commonly an HTML error page, an empty string, or a plain-text message.

## Meaning

The server returned HTML (404/500 page, login redirect, proxy error) but the app tried to parse it as JSON. Check status code and Content-Type, log the body, and confirm the URL. Trailing commas or single quotes also cause `Unexpected character`.

## Common causes

- Server returned HTML for an error or redirect
- Wrong URL or missing API path
- Empty body
- Invalid JSON (single quotes, trailing comma)
- Gzip/encoding problem

## Quick fix

1. Print `response.statusCode` and `response.body`
2. Verify the URL in a browser or curl
3. Only decode when status is 200 and Content-Type is JSON

## Detailed fix by platform

### Dart

1. `if (res.statusCode != 200) throw Exception('HTTP ${res.statusCode}: ${res.body}');`

## How to diagnose

1. **Body** — HTML or JSON?
2. **Status** — 200?
3. **URL** — Correct?

---
Source: [WhatIsMyError](https://whatismyerror.com/errors/flutter/format-exception-unexpected-character)
