Response Handling & Mocking in Martini
This section discusses how to handle response headers and mock responses in Martini, enhancing the API development process.
Response Headers
Adding Response Headers
In Martini, adding response headers is straightforward. You can include extra information in the headers without cluttering the response body.
- Creating Output Model: In your service, create an output model. Add properties matching the header names you want to return.
- Populating Headers: Ensure that these properties are populated with the desired values.
Here is an example of a model configured to return Content-Type
and X-API-Limit
headers.
Object Aliases for Headers
If you use an invalid Java or Groovy name for a Gloop property, Martini automatically assigns an alias.
- Mapping Headers: To map headers to a model, select the Headers node in the response section of the editor. Choose an output model for the headers.
Mocking Responses
Martini supports an API-first approach, allowing you to define API specifications before coding. This approach ensures a clear contract for what the API will provide.
Enabling Data Mocking
- Mock Property: Enable data mocking for a Gloop REST API operation by setting the
Mock
property totrue
. This changes the operation node's label to include(Mock)
.
Configuring Mock Response Payload
- Payload Formats: You can set mock responses in text, JSON, XML, or YAML. Ensure the format matches the one specified in the
Produces
node. - Payload Content: Your payload can be any content in the chosen format. For randomized payloads, Martini will generate random values.
Response Code Field
- Determining Response: The
Response Code Field
under the Responses node is used by Gloop at runtime to determine the appropriate response. - Return Value: Services should return only the status code in this property. If it’s null or not numeric, Martini logs a warning and assumes a 200 status code.