MIME Types and Content-Type
MIME (Multipurpose Internet Mail Extensions) types tell the client and server what format data is in. The Content-Type header specifies the MIME type of the content.
How Content-Type Works
Request:
POST /api/users
Content-Type: application/json
_________________________/
│ MIME type (type/subtype)
│
├── type: application (application data)
├── subtype: json (JSON format)
Common MIME Types
| Type | Subtype | Content-Type Header |
|---|---|---|
| Text | HTML | text/html |
| Text | Plain | text/plain |
| Text | CSS | text/css |
| Text | JavaScript | text/javascript |
| Application | JSON | application/json |
| Application | XML | application/xml |
| Application | Form | application/x-www-form-urlencoded |
| Application | application/pdf | |
| Image | PNG | image/png |
| Image | JPEG | image/jpeg |
| Multipart | Form Data | multipart/form-data |
Content-Type in Practice
# JSON API
POST /api/users
Content-Type: application/json
{"name": "Alice"}
# Form submission
POST /login
Content-Type: application/x-www-form-urlencoded
username=alice&password=secret
# File upload
POST /api/upload
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary
Content Negotiation
Clients and servers negotiate content type:
Client sends:
Accept: application/json, text/plain;q=0.9
Server responds:
Content-Type: application/json
If server can't provide JSON:
Content-Type: text/plain
(Vary: Accept header)
Why Content-Type Matters
- Security: Wrong Content-Type can lead to XSS or MIME sniffing
- Parsing: Server must parse body according to Content-Type
- API contracts: APIs define expected Content-Type
- Browser behavior: Browsers render based on Content-Type
Best Practices
Key Principles
- Follow SOLID principles
- Write clean, readable code
- Test thoroughly
- Document decisions
- Monitor in production
Implementation
- Start simple, refactor as needed
- Use established patterns
- Consider trade-offs
- Review with peers
Continuous Improvement
- Learn from incidents
- Update documentation
- Share knowledge
- Mentor others
Key Points
- Understanding Content Types is essential for production systems
- Always consider scalability and maintainability
- Test thoroughly before deploying to production
- Monitor performance and set up alerting
Common Patterns
- Validation: Always validate input at the boundary
- Error Handling: Use structured error responses
- Logging: Log key events for debugging
- Testing: Unit, integration, and load tests
- Documentation: Keep docs updated with code changes
Practice Problems
Design and implement a solution for Content Types in a backend system. Consider scalability, error handling, and production readiness.
Solution
// Content Types implementation
// Key aspects: validation, error handling, logging, testing
public class ContentTypes {
// Production-ready implementation
} Identify and handle edge cases for Content Types. What happens under high load, with invalid input, or during failures?
Solution
// Edge case handling:
// 1. Null/empty input -> validation
// 2. High load -> rate limiting, queuing
// 3. Failures -> retries, circuit breaker
// 4. Concurrent access -> locks, idempotency Write a testing strategy for Content Types. Include unit tests, integration tests, and performance tests.
Solution
// Test plan:
// - Unit: 80% coverage target
// - Integration: API contracts
// - Performance: latency, throughput
// - Chaos: failure injection Quiz
1. What MIME type is used for JSON APIs?
2. What does Content-Type: multipart/form-data indicate?
3. What is a common mistake when implementing Content Types?
Flashcards
Question
What is a MIME type?
Click to reveal answer
Answer
A standard identifier indicating the format of data (type/subtype)
Question
What Content-Type for JSON?
Click to reveal answer
Answer
application/json
Question
Content Types best practices
Click to reveal answer
Answer
Follow SOLID principles, write clean code, test thoroughly, document decisions, and monitor in production.
Revision Notes
Key Takeaways
- 1. Content-Type tells the client/server the data format
- 2. JSON uses application/json
- 3. Form data uses application/x-www-form-urlencoded or multipart/form-data
- 4. Content negotiation uses Accept header
Interview Tips
- • Know the correct MIME types for common formats
- • Understand content negotiation
Cheat Sheet
Content Types
- JSON: application/json
- Form: application/x-www-form-urlencoded
- File Upload: multipart/form-data
- HTML: text/html
- Negotiation: Accept header → Content-Type response