Search code examples
wso2ballerina

How to read the single header value when it contains special characters in Ballerina


I need to read the "X-JWT-Assertion" with the request.

Usually, we could use the following technique to read "Connection" header parameter.

resource function get menu(@http:Header string Connection) returns json {}

But when the header value contains special characters, it gives an error. Error

resource function get menu(@http:Header string X-JWT-Assertion) returns json {}

How to use refer to values with special characters in parameters in the resource definition?


Solution

  • You have to use \ to escape -.

    E.g.,

    import ballerina/http;
    
    service on new http:Listener(8080) {
        resource function get menu(@http:Header string X\-JWT\-Assertion) returns json {
    
        }
    }
    

    Ballerina example - Identifiers