How to define multiple response codes

You can define multiple HTTP response codes to quickly determine the results of a REST API operation. Defining multiple response codes also allows you to efficiently detect and handle errors with your REST API. You can define custom HTTP headers and customize the HTTP response body to relay the appropriate information from your REST API.

Before you begin

zosConnect-2.0 Applies to zosConnect-2.0.

If your API project is not yet open in the z/OS® Connect API editor, in the Project Explorer view, open the package.xml file that is located in your API project.

Restriction:

To successfully create and deploy an API by using multiple response codes, make sure that you are using the minimum required version of z/OS Connect, version 3.0.16.

About this task

This task guides you step by step how to define multiple HTTP response codes to allow you to quickly determine the results of a REST API operation. The following image provides

Procedure

  1. In the API editor, click Mapping... > Define Response Codes for the method you want to add a response to.
    Response details for your API method are shown. A default response code, 200, is preconfigured for each API method.
  2. Under Responses, click Add Response (Add Response) to define a new HTTP response code.
    The Add Response window opens.
  3. In the Response code field of the Add Response window, specify an HTTP response code. Select a response code by using the drop-down list, or alternatively, specify your own response code in the numeric range of 100 to 599 by typing into the text field.
  4. Use the Description field to specify the purpose of the response code.
  5. Define the rules that are used to determine when this response code is issued. A rule is composed of (from left to right) a name, a service response field, a comparison operator, and a comparison value.
    1. In the Rule name field, specify a short name that identifies the rule.
    2. In the service field drop down box, select a service response field to compare against.
      Restriction: Array types cannot be used for a service field.
    3. In the comparison operator drop down box, select an operator to compare the service field value against the comparison value.
      Comparison operators change according to the data type specified in the service field box.
    4. In the comparison value field, specify a value to be compared with the corresponding service field value.
    The structure of rules that are used for multiple response codes typically mimics the conventions that are used for performing programmatic comparisons in Java. To see a demonstration of rules used for multiple response codes, see the IMS API example Define contacts API response codes.
    Restriction: Array-type service fields cannot be used for rule definition.
  6. When you are finished specifying rules for your response, click OK.
    Important:
    • To later edit the rules for a given response, return to the Add Response window by clicking Response... > Edit Response.
    • To delete a rule, click Delete rule (Delete rule) for that rule.
    • Rule errors prevent you from saving a response code. To resolve errors within the Add Response window, mouse over any error indicators to be prompted with the solution.
  7. Optional: Define a unique service response JSON mapping for when this response code is issued.
    • To configure unique response mapping for an API operation, click Mapping... > Open Response Mapping for the response you want to edit.
    • To delete a unique response mapping, and revert to the default response mapping, click Mapping... > Clear Response Mapping for the response you want to delete.
    Specifying a unique service response mapping returns the appropriate information for a given response. For more information about response field mapping, see Defining HTTP-to-JSON mapping.
    Important:

    If you do not specify a unique service response mapping, the default 1-to-1 mapping between the service and API response fields are used.

  8. Specify the order in which response codes are evaluated. Click the up (Increase precedence) and down (Decrease precedence) arrow buttons for each response to increase or decrease the precedence of evaluation for each response code. The response with the lowest precedence is marked as the default response code.
    Important:

    Responses are interpreted sequentially, in the order of highest precedence (first) to the lowest precedence (last). During rule evaluation, all service response fields are tested against the rules that are defined for the API method. After rule evaluation, the API response field mapping is completed.

    During an API operation, if none of the rule sets for each response evaluates to true, the default response code is issued. The default response is marked with a checkmark next to the response code.

  9. When you are finished, click File > Save (Crtl-S) to save changes that are made to your API methods.

Results

You have created multiple response codes for an API operation that are issued according to their specified rule evaluation.

What to do next

Deploy an API.