Skip to main content
Rate this page

Generic HTTP Client

What Is the Generic HTTP Client?

In the Executing a request using a generated OData client section, we explained how to make OData requests using the generated OData client. This client is the highest layer of convenience and type-safety for the user. Below the OData client uses the Generic HTTP client of the SAP Cloud SDK. For the final HTTP calls, we have decided to use the axios library.

The layers of HTTP clients in the SDK.

In this document, we explain when and how to use the middle layer directly. Different clients serve different purposes:

OData Client: Provides strong typing for the request (payload, select, filter, etc.) and response. For each OData service, a separate client needs to be generated which defines the types and methods for this service. The client takes care of deserializing your request to a URL and JSON payload as well as serializing the response. The generated payload and URL are passed to the Generic HTTP Client.

Generic HTTP Client: Adds SAP infrastructure specific functionality on top of a standard HTTP Client. All OData services use the same generic HTTP client, so it contains no service-specific information. The client handles connectivity related issues such as:

In the end, all information from the destination, connectivity service, and proxy configuration ends up in header-fields and proxy-agents. The information goes one level down to the axios client.

axios HTTP Client: Is a widely used open source HTTP client for node. This client executes the actual HTTP requests in the end.

How to Use It

To make a request using the Generic HTTP client use the executeHttpRequest function.

executeHttpRequest(destination, requestConfig, httpRequestOptions);

The destination argument is either a full destination object, that you have already fetched or an object containing a destination name and an optional JWT. In the latter case, the SAP Cloud SDK fetches the destination for you. To learn more, check the destination documentation. It provides more details about the JWT parameter. The request configuration argument contains the request configuration. A minimal configuration would look like this:

{
method: 'get',
params: {
a: 'a',
b: 'b'
},
...
}

Note that you can also give values for url in the request configuration. The values you give in the request configuration will be merged with ones related to the destination:

{
...destinationRequestConfig,
...customRequestConfig,
headers: {
...destinationRequestConfig.headers,
...customRequestConfig.headers
}

For keys which exist for both object the value from the custom request configuration will be used. For example, a request configuration with authorization headers will overwrite the authorization header information from the destination.

You can also pass an optional third parameter httpRequestOptions, where you can indicate for example whether the SAP Cloud SDK should fetch CSRF token for a non-GET request. For a get request, the CSRF token is not fetched and this option is ignored. Below is an example of the httpRequestOptions to delegate the CSRF token fetching to the SAP Cloud SDK.

{
fetchCsrfToken: true
}

By default, the value of fetchCsrfToken is false.

When to Use It

You should consider the Generic HTTP client if:

  • You need to use an unsupported feature by the typed OData Client like upsert for example. With this, you can profit from the aforementioned convenience in connectivity.
  • You want to ping a service or trigger a function import without a complicated payload. In such a case the size of a full data model of the typed client is perhaps not worth the benefits.
  • You want to call a non-OData service that has no service definition.

You should consider the OData client if:

  • You have to build complicated filter, selection, and/or expand conditions. Here you will highly benefit from the help of the OData client.
  • You want to update or create new entities. The OData Client has built-in ETag versions handling. With the generic client, you have to manage versions on your own.
Rate this page