Content-Type e o cabeçalho Accept
Entenda por que as respostas JSON anunciam application/json.
Content-Type e o cabeçalho Accept é uma aula grátis de Flask Academy no CoddyKit. Esta é a aula 4 de 4. Você pode ler a aula completa abaixo gratuitamente — depois pratica ao vivo no navegador com um editor de código integrado e um tutor de IA 24/7. Faz parte do caminho de aprendizado de Flask Academy, e seu progresso é sincronizado entre a web e o app CoddyKit. O curso de Flask Academy inclui 4 aulas no total.
Partes desta aula ainda não foram traduzidas e aparecem em inglês.
Headers Carry Meaning
A response is more than a body. HTTP headers describe that body, and two of them decide how JSON flows between client and server. 📨
What Content-Type Says
The Content-Type header announces the format of the body you are sending. For JSON replies its value is application/json.
Content-Type: application/jsonjsonify Sets It for You
You rarely set this header by hand. When you call jsonify, Flask attaches application/json automatically, so clients always parse correctly.
Why It Matters to Clients
A browser or library reads Content-Type to decide how to handle the body. Seeing application/json, it parses text into a usable object.
Wrong Type, Wrong Result
Return JSON text as plain text/html and clients may not parse it. The body looks right but the missing signal breaks automatic decoding.
What the Accept Header Says
The request can carry an Accept header that lists formats the client wants. It is a wish, telling your server which response type fits best.
Accept: application/jsonContent-Type vs Accept
Keep the pair straight: Content-Type describes the body in the current message, while Accept states what the client hopes to receive back.
Reading Accept in Flask
You can inspect the request to honor what the caller wants. The request.accept_mimetypes proxy reveals the formats listed in that header.
request.accept_mimetypes["application/json"]Content Negotiation
Serving the format a client asked for is called content negotiation. An API can return JSON or HTML for the same route based on Accept.
Request Bodies Have Types Too
Incoming requests also carry Content-Type. When a client sends application/json, Flask knows the body is JSON and request.get_json can parse it.
Trust the Defaults
For most JSON APIs, jsonify handling Content-Type is all you need. Reach for Accept only when one route must serve multiple formats.
Quick Check
Tell apart the two headers that shape JSON exchange.
Recap
You learned that Content-Type labels the body and Accept asks for a format, and that jsonify sets application/json so JSON clients just work. 🎉
Perguntas Frequentes
A aula “Content-Type e o cabeçalho Accept” é grátis?
Sim — o texto completo de “Content-Type e o cabeçalho Accept” é grátis para ler aqui na web. Para praticá-la interativamente (um editor de código integrado e um tutor de IA 24/7) e desbloquear o restante do curso de Flask Academy, atualize para CoddyKit PRO. O curso de Flask Academy inclui 4 aulas no total.
O que vou aprender em “Content-Type e o cabeçalho Accept”?
Entenda por que as respostas JSON anunciam application/json. Você pratica Flask Academy com código prático que executa diretamente no navegador, e um tutor de IA 24/7 responde suas dúvidas enquanto trabalha na aula.
Preciso ter experiência prévia para começar Flask Academy?
Nenhuma experiência prévia é necessária. Flask Academy no CoddyKit é estruturado para alunos iniciantes até avançados, então você pode começar aqui ou desde o início e aprender no seu ritmo. Esta é a aula 4 de 4.
Quanto tempo leva a aula “Content-Type e o cabeçalho Accept”?
A maioria das aulas CoddyKit leva cerca de 5–10 minutos. Cada uma é compacta e interativa, então você faz progresso constante e retoma exatamente de onde parou entre web e app.
Posso escrever e executar código nesta aula de Flask Academy?
Sim. Cada aula de Flask Academy inclui um editor de código integrado, então você escreve e executa código real direto no navegador e recebe feedback de IA instantaneamente — nenhuma configuração local necessária.
Todas as aulas deste curso
- Converta um dicionário com jsonify
- Retorne listas e dados aninhados
- Defina códigos de status nas respostas JSON
- Content-Type e o cabeçalho Accept