Invalid RestResponse headers
Why is this an issue?
Starting with Spring '24, Salesforce enforces RFC 7230 validation for all REST response headers defined via RestResponse.addHeader(). Headers with invalid names will cause a runtime InvalidHeaderException.
RFC 7230 defines header names as "tokens" which may only contain these characters:
Letters (a-z, A-Z)
Digits (0-9)
Special characters:
!,#,$,%,&,',*,+,-,.,^,_,`,|,~
The following delimiter characters are not allowed:
Whitespace (spaces, tabs)
Delimiters:
",(,),,,/,:,;,<,=,>,?,@,[,\,],{,}
Examples
Example of incorrect code:
@RestResource(urlMapping='/example/*')
global class MyRestService {
@HttpGet
global static void doGet() {
RestResponse res = RestContext.response;
res.addHeader('My Header', 'value'); // Space not allowed
res.addHeader('Content/Type', 'value'); // Slash not allowed
}
}
Example of correct code:
@RestResource(urlMapping='/example/*')
global class MyRestService {
@HttpGet
global static void doGet() {
RestResponse res = RestContext.response;
res.addHeader('X-Custom-Header', 'value'); // Hyphens are valid
res.addHeader('X_Custom_Header', 'value'); // Underscores are valid
}
}
How can I fix violations?
Replace invalid characters in header names:
Remove spaces or replace with hyphens
Remove delimiter characters:
"(),/:;<=>?@[\]{}
Resources
