Response Body
Configure the response body for your mocked HTTP calls.
Overview
HTTP Mock Lib supports three types of response bodies:
- String - Raw string data
- Object - Apex objects (automatically serialized to JSON)
- Blob - Binary data
API
body(String)
Set a string response body.
HttpMock body(String body)Example:
new HttpMock()
.whenGetOn('/api/users')
.body('{"id": "123", "name": "John Doe"}')
.statusCodeOk()
.mock();body(Object)
Set an object response body. The object will be JSON-serialized automatically.
HttpMock body(Object body)Example:
Map<String, Object> response = new Map<String, Object>{
'id' => '123',
'name' => 'John Doe',
'email' => 'john@example.com'
};
new HttpMock()
.whenGetOn('/api/users/123')
.body(response)
.statusCodeOk()
.mock();body(Blob)
Set a binary response body.
HttpMock body(Blob body)Example:
Blob pdfData = Blob.valueOf('PDF content here');
new HttpMock()
.whenGetOn('/api/documents/123')
.body(pdfData)
.contentTypePdf()
.statusCodeOk()
.mock();Examples
JSON String Response
new HttpMock()
.whenGetOn('/api/v1/token')
.body('{"access_token": "abc123", "expires_in": 3600}')
.contentTypeJson()
.statusCodeOk()
.mock();Map Response
Map<String, String> tokenResponse = new Map<String, String>{
'access_token' => 'abc123',
'expires_in' => '3600'
};
new HttpMock()
.whenGetOn('/api/v1/token')
.body(tokenResponse)
.statusCodeOk()
.mock();List Response
List<Map<String, String>> users = new List<Map<String, String>>{
new Map<String, String>{ 'id' => '1', 'name' => 'Alice' },
new Map<String, String>{ 'id' => '2', 'name' => 'Bob' }
};
new HttpMock()
.whenGetOn('/api/users')
.body(users)
.statusCodeOk()
.mock();Custom Class Response
public class UserResponse {
public String id;
public String name;
public String email;
}
UserResponse user = new UserResponse();
user.id = '123';
user.name = 'John Doe';
user.email = 'john@example.com';
new HttpMock()
.whenGetOn('/api/users/123')
.body(user)
.statusCodeOk()
.mock();XML Response
String xmlResponse = '<?xml version="1.0"?>' +
'<user>' +
'<id>123</id>' +
'<name>John Doe</name>' +
'</user>';
new HttpMock()
.whenGetOn('/api/users/123')
.body(xmlResponse)
.contentTypeXml()
.statusCodeOk()
.mock();Empty Response
For responses with no body (like DELETE operations):
new HttpMock()
.whenDeleteOn('/api/users/123')
.statusCodeNoContent()
.mock();
// No .body() call neededBinary Response (PDF)
Blob pdfContent = generatePdfContent();
new HttpMock()
.whenGetOn('/api/reports/monthly.pdf')
.body(pdfContent)
.contentTypePdf()
.header('Content-Disposition', 'attachment; filename="report.pdf"')
.statusCodeOk()
.mock();Object Serialization
When using body(Object), HTTP Mock Lib uses JSON.serialize() to convert your object:
// This code:
new HttpMock()
.body(new Map<String, String>{ 'key' => 'value' })
.mock();
// Is equivalent to:
new HttpMock()
.body('{"key":"value"}')
.mock();Default Behavior
If you don't call .body(), the response body will be an empty string:
new HttpMock()
.whenGetOn('/api/ping')
.statusCodeOk()
.mock();
// Response body: ""Best Practices
Use Objects for Complex Data - For complex responses, use Maps or custom classes instead of building JSON strings
Match Real Responses - Use response formats that match your actual API
Set Appropriate Content-Type - Always set the correct content type for your response body
Validate JSON - If using string bodies, ensure your JSON is valid
Test Edge Cases - Mock empty responses, large responses, and malformed responses
Common Patterns
Paginated Response
Map<String, Object> paginatedResponse = new Map<String, Object>{
'data' => new List<Map<String, String>>{
new Map<String, String>{ 'id' => '1' },
new Map<String, String>{ 'id' => '2' }
},
'page' => 1,
'total_pages' => 5,
'total_count' => 50
};
new HttpMock()
.whenGetOn('/api/users?page=1')
.body(paginatedResponse)
.statusCodeOk()
.mock();Error Response
Map<String, Object> errorResponse = new Map<String, Object>{
'error' => new Map<String, String>{
'code' => 'INVALID_REQUEST',
'message' => 'Email is required'
}
};
new HttpMock()
.whenPostOn('/api/users')
.body(errorResponse)
.statusCodeBadRequest()
.mock();