Custom Headers in Trongate MX
The mx-headers
attribute in Trongate MX allows you to specify custom headers for your HTTP requests. This feature provides flexibility in managing request headers, which can be useful for tasks such as content negotiation, custom server behaviors, and more.
Syntax
<element mx-headers='{
"Header-Name": "Header-Value",
"Another-Header": "Another-Value"
}'></element>
The mx-headers
attribute should contain a JSON object, where each key-value pair represents a header and its value.
Usage
To use the mx-headers
attribute:
- Add the attribute to an element that triggers an AJAX request (e.g., a button with
mx-get
ormx-post
). - Specify the headers in a JSON object format.
Example
<button mx-get="api/resource"
mx-headers='{
"Client-ID": "12345",
"App-Version": "1.0"
}'>
Fetch Resource
</button>
In this example:
- Clicking the "Fetch Resource" button triggers an AJAX GET request to the specified URL.
- The request includes custom headers for client identification and application version.
Best Practices
- Content Negotiation: Use headers like
Accept
andContent-Type
to specify preferred content types and formats. - Custom Behaviors: Implement custom headers to trigger specific behaviors on your server or for tracking purposes.
- Modularity: Define reusable headers in a consistent format to maintain clarity and modularity in your code.
Additional Information
- The
mx-headers
attribute should contain a valid JSON object. Each key-value pair represents a header and its value. - Ensure that the JSON string is properly formatted to avoid parsing errors.
- The headers specified in
mx-headers
will be added to the HTTP request along with any default headers set by Trongate MX.
Additional Notes
- The
mx-headers
attribute is parsed and applied to the HTTP request before it is sent. - If the JSON string in the
mx-headers
attribute is invalid, an error will be logged to the console, and the headers will not be applied. - For token-based authentication, use the
mx-token
attribute instead ofmx-headers
. - Custom headers can be used to manage various aspects of HTTP communication, such as content negotiation and custom server behaviors.
By utilizing the mx-headers
attribute, you can enhance the flexibility and control of your HTTP requests in Trongate MX, making it easier to manage custom headers and improve the robustness of your web applications.