Header fields
Header fields are part of an HTTP message and are used to exchange additional information between the client and the server. They do not describe the actual payload content, but rather provide metadata about the request or response. Typical examples include information about the data format, authentication, the desired compression, or the behavior of the connection.
A header field consists of a field name and an associated value. The field name describes the meaning of the entry, while the value contains the specific information. Examples include Content-Type, Accept, Authorization, and User-Agent.
Accept: application/json
Content-Type: application/json
Authorization: Bearer <Token>
User-Agent: TwinCAT HTTP Client
Host: example.com
Connection: close
Cache-Control: no-cache
Accept-Encoding: gzipIn an HTTP request, header fields can be used to provide the server with additional information for processing the request. This includes, for example, the data format of the content being sent or the desired format of the response. JSON is frequently used in REST APIs. In this case, “Content-Type: application/json” indicates that the body contains JSON data, while “Accept: application/json” signals that the response is also expected to be in JSON format. Authentication information – such as a bearer token in the Authorization field – is also frequently transmitted via header fields.
In TwinCAT, additional header fields can be added to an HTTP request. This makes it possible to incorporate the requirements of a REST API directly from the PLC context. The header fields are created using the FB_IotHttpHeaderFieldMap function block before the request is sent and are then passed to the HTTP command.
Automatically set header fields
Certain header fields are generated automatically by TwinCAT or derived from the parameters of the function blocks used. These fields are locked to prevent manual editing by the user, in order to ensure a consistent HTTP request. For such header fields, you must use the designated properties or inputs of the respective function blocks. Additional header fields required by the server can be added using the function block.
Below is a list of the header fields with limited editability:
- Accept-Encoding: is automatically set to gzip, deflate.
- Connection: either “keep-alive” or “close”; this is set via the FB_IotHttpClient.
- Content-Length: is automatically set for requests with a payload.
- Content-Type: is set via the FB_IotHttpRequest.
- Host: consists of the host name and port configured in FB_IotHttpClient.