REST Service
The REST Service source is a multithreaded source that processes all authorized REST API requests. The source can also send records with status codes back to the originating REST API client when used with one or more Send Response to Source targets in the same flow.
By default, the REST Service source listens at an HTTP endpoint for requests. You can configure the source to use Data Collector as an API gateway, which enables using the Data Collector URL as an API endpoint.
Use the REST Service source to create a REST-based microservice. In a microservice flow, the REST Service source works with one or more microservice targets that specify the HTTP response code to pass back to the originating REST API client.
The REST Service source generates a batch for each request that it receives. The REST Service source can use multiple threads to enable parallel processing of requests from multiple clients. The source can also read requests containing messages with no compression or with the Gzip or Snappy compression format.
When using the REST Service source, you must configure the REST API clients to send data to the flow. The required client configuration depends on how you configure the source.
When you configure the REST Service source, you decide if you want to use a listening port or an API gateway to receive requests. To use a listening port, you specify the HTTP listening port. To use an API gateway, you specify the service name and whether to require gateway authentication.
When not using gateway authentication, you can enable Kerberos authentication using SPNEGO, or SSL/TLS with basic authentication.
For additional security, you can require each request to include a valid application ID by listing valid application IDs in the source. And you can indicate if the application ID will be in the URL instead of the request header.
You can configure the maximum number of concurrent requests to determine how many threads to use. You also configure the format of generated responses.
When you start the flow, Data Collector displays the endpoint that the flow uses in monitor mode.
HTTP listening port
By default, the REST Service source listens for requests at an HTTP
endpoint and processes all authorized requests. You can specify the port number that the source listens at. By default, the source uses port number
8000.
The specified listening port should be used by a single flow, unless you use application IDs to differentiate between the requests to be processed by different flows.
API gateway
You can configure the source to use Data Collector as an API gateway instead of listening for messages at an HTTP endpoint.
When you enable Data Collector as an API gateway, you specify a service name for the flow, which Data Collector uses to route requests to the flow. Then, you configure REST API clients to send requests to the specified service name.
Gateway authentication
When the REST Service source uses Data Collector as an API gateway, you can also configure the source to require gateway authentication.
When you require gateway authentication, the user in each request must be an authenticated IBM watsonx platform user. This leverages the authentication method configured for IBM watsonx platform. When you enable gateway authentication, the source ignores other user authentication options configured in stage properties.
REST API URLs
- Secure API
- Use the secure REST API when the source requires gateway authentication.
- Public API
- Use the public REST API when the source does not require gateway authentication. The public API can be accessed by any user, but to pass requests to the flow, the user must have read permission for the flow.
For more information about constructing the URL, see REST API client requirement.
Required request header
"X-Requested-By:<user-defined string>"This header is used by the flow to provide additional security. The user-defined string can be any string value.
userService
gateway service using client as the
requestor:curl -i -X POST http://localhost:18630/public-rest/v1/gateway/userService/user
--header "X-SDC-APPLICATION-ID:microservice" --header "X-Requested-By:client" -d ‘{"Name": "Joe"}’Security
- Application ID
- When you specify a list of application IDs, the source processes only incoming requests that include one of the specified application IDs.
- Gateway authentication
- When the source requires gateway authentication, the source uses the Data Collector SSL/TLS properties, if configured. The source also requires requests to use the secure REST API.
- Kerberos with SPNEGO
- The source can authenticate using Kerberos with SPNEGO web authentication. You can use this option when your environment is enabled to perform Kerberos authentication over HTTP.
- SSL/TLS with additional options
- When you enable SSL/TLS in the source, you can also enable the following
additional security measures:
- Client authentication - Requires mutual authentication of each REST API client before accepting requests.
- Basic authentication - Requires incoming requests to include one of
the user accounts specified in the source.
You can use basic authentication when other authentication methods, such as gateway or Kerberos with SPNEGO authentication, are not enabled. When other authentication methods are enabled, basic authentication properties are ignored.
Using application IDs
You can configure the REST Service source to only process requests that include a valid application ID. To require application IDs in the requests, define a list of valid application IDs in the REST Service source.
When you configure application IDs for the source, you must configure the REST API clients to include one of the listed application IDs in each request.
You might use application IDs to provide additional security or to enable multiple flows to listen at the same port. That is, if you specify a different application ID for each flow, multiple flows can safely share the same port.
Include the application ID for each request in one of the following ways:
- In request headers
- Add the following information to the HTTP request header for all requests that
you want the source to
process:
X-SDC-APPLICATION-ID: <application_ID> - In a query parameter in the URL
- If you cannot configure the client request headers - for example if the requests are generated by another system - then configure each REST API client to send data to a URL that includes the application ID in a query parameter.
REST API client requirement
To enable the REST Service source to process data, you must configure the REST API clients to send data to the flow. The URL that you use depends on how you configure the source.
- Listening port
- When the source is configured to listen for data at a specified
port, you must configure each client to send data to a URL that includes the listening port
number.Note: No other flows or processes can already be bound to the listening port. The listening port can be used only by a single flow.Use the following format for the URL:
<http | https>://<sdc_hostname>:<listening_port>/<path>?<queryString>&sdcApplicationId=<application_ID>The URL includes the following components:- <http | https> - Use
httpsfor secure HTTP connections. - <sdc_hostname> - The Data Collector host name.
- <listening_port> - The port number where the source listens for data.
- <path> - Optional. The path of the URL.
- <queryString> - Optional. The parameters of the URL that come after the path.
- <application_ID> - Optional application ID if not passed in the request header.
For example:
https://localhost:8000/orhttps://localhost:8000/rest/v1/user. - <http | https> - Use
- API gateway
- When the source is configured to use Data Collector as an API gateway, you specify a service name to use and indicate if authentication is required.
Multithreaded processing
The REST Service source performs parallel processing and enables the creation of a multithreaded flow.
The REST Service source uses multiple concurrent threads based on the Max Concurrent Requests property. When you start the flow, the source creates the number of threads specified in the Max Concurrent Requests property. Each thread generates a batch from an incoming request and passes the batch to an available flow runner.
A flow runner is a sourceless flow instance - an instance of the flow that includes all of the processors, executors, and targets in the flow and handles all flow processing after the source. Each flow runner processes one batch at a time, just like a flow that runs on a single thread. When the flow of data slows, the flow runners wait idly until they are needed, generating an empty batch at regular intervals. You can configure the Runner Idle Time flow property to specify the interval or to opt out of empty batch generation.
Multithreaded flows preserve the order of records within each batch, just like a single-threaded flow. But since batches are processed by different flow runners, the order that batches are written to targets is not ensured.
For example, say you set the Max Concurrent Requests property to 5. When you start the flow, the source creates five threads, and Data Collector creates a matching number of flow runners. Upon receiving data, the source passes a batch to each of the flow runners for processing. In the batch, REST Service includes only the REST API requests with the specified application ID.
Each flow runner performs the processing associated with the rest of the flow. After a batch is written to flow targets, the flow runner becomes available for another batch of data. Each batch is processed and written as quickly as possible, independent from other batches processed by other flow runners, so batches may be written differently from the read order.
At any given moment, the five flow runners can each process a batch, so this multithreaded flow processes up to five batches at a time. When incoming data slows, the flow runners sit idle, available for use as soon as the data flow increases.
For more information about multithreaded flows, see Multithreaded flow overview.
Generated response
In a microservice flow, the REST Service source can send a response back to the originating REST API client.
- Records received from microservice targets
- Flow error records received when the flow is configured to use the Send Response to Source flow error handling
The source generates a single response for each batch of records received. The source can generate the response in JSON or XML format. The response can include an envelope or only raw data.
Responses with an envelope
| Response Key or Element | Value |
|---|---|
| httpStatusCode | The status code associated with the records in the
response. If the records in the generated response share the same status code, the code is written to the httpStatusCode key or element. If the records have different status codes, the httpStatusCode is set to 207 for multiple statuses. |
| data | A list of records passed to the source by the microservice targets used in the flow. |
| error | A list of flow error records passed to the source by the Send Response to Source flow error handling. |
| errorMessage | The error message associated with the first error
record in the response. Used only when the response includes error records. |
{
"httpStatusCode":<status code>,
"data":[<list of success records>],
"error":[<list of error records>],
"errorMessage": <error message, if any>
}Raw responses
When configured to send a raw response, the source generates a response that contains either the list of records passed from the microservice targets or the list of error records passed by the Send Response to Source flow error handling. If the source receives data records from targets and error records from the flow, then the source includes only the error records in the response. If the source receives no data records from targets and no error records from the flow, then the source generates an empty response.
Sample responses
- Single record
- The source receives a single record from the Send Response to Source
target. The target is configured to use the 200 status code.For a response with an envelope, the source sends the following response:
{ "httpStatusCode":200, "data":[{"ID":"103","NAME":"Jack","AGE":"37","STATE":"MD"}], "error":[], "errorMessage":null }For a raw response, the source sends the following response:{"ID":"103","NAME":"Jack","AGE":"37","STATE":"MD"} - Multiple data and error records
- The source receives several records, data and error. Because each record has
a different status code, the response uses status code 207 for multiple
statuses. The errorMessage key includes the error associated with the first
record which has a missing ID. The source is configured to present multiple
records as multiple JSON objects.For a response with an envelope, the source sends the following response:
{ "httpStatusCode":207, "data":[{"ID":"248","NAME":"Pina","AGE":"24","STATE":"RI"}], "error":[{"NAME":"Liz","AGE":"37","STATE":"DE"}, {"ID":"302","NAME":"Roco","AGE":"","STATE":"CA"}], "errorMessage":"COMMON_0001 - Stage precondition: CONTAINER_0051 - Unsatisfied precondition(s) '${record:exists('/ID')}'" }For a raw response, the source sends the following response:{"NAME":"Liz","AGE":"37","STATE":"DE"}, {"ID":"302","NAME":"Roco","AGE":"","STATE":"CA"}
Record header attributes
The REST Service source creates record header attributes that include information about the requested URL.
You can use the record:attribute or
record:attributeOrDefault functions to access the information
in the attributes. For more information about working with record header attributes,
see Working with header attributes.
- method - The method for the request, such as GET, POST, or DELETE.
- path - The path of the URL.
- queryString - The parameters of the URL that come after the path.
- remoteHost - The name of the client or proxy that made the request.
The REST Service source also includes HTTP request header fields – such as Host or Content-Type – in records as record header attributes. The attribute names match the original HTTP request header field name.
Data formats
The REST Service source processes data differently based on the data format that you select. The source processes the following types of data:
- Avro
- Generates a record for every Avro record. The source includes the Avro schema in the
avroSchemarecord header attribute. It also includes aprecisionandscalefield attribute for each Decimal field. - Binary
- Generates a record with a single byte array field at the root of the record.
- Datagram
- Generates a record for every message. The source can process collectd messages, NetFlow 5 and NetFlow 9 messages, and the following types of syslog messages:
- Delimited
- Generates a record for each delimited line.
- JSON
- Generates a record for each JSON object. You can process JSON files that include multiple JSON objects or a single JSON array.
- Protobuf
- Generates a record for every protobuf message. By default, the source assumes messages contain multiple protobuf messages.
- SDC Record
- Generates a record for every record. Use to process records generated by a Data Collector flow using the SDC Record data format.
- XML
- Generates records based on a user-defined delimiter element. Use an XML element directly under the root element or define a simplified XPath expression. If you do not define a delimiter element, the source treats the XML file as a single record.
Configuring a REST Service source
About this task
Configure a REST Service source to process REST API requests and pass responses back to the originating REST API client. Use the source as part of a microservice flow.