I'm trying to build a slack integration with an apex rest endpoint. and I need to get a message in a format such as below. Normally I would create an object and return the object, however slack uses the key "short" which is reserved in apex. I tried to generate an object, then JSON.serialize and do a replace (ex. short1 > short) and I've also tried using the JSON.generator to manually create the request but every method has escaped double quotes in the response which shows it as text instead of the proper format. I'd appreciate it if someone could help me figure out how to return a non-escaped JSON string that includes reserved (short) characters. Thanks in advance.
Response I'm getting ex. "{\n \"text\" : \"sample text\"\n}"
Final Response I'm trying to generate (note short in fields) { "attachments": [ { "color": "#01a3d6", "fields": [ { "title": "Status", "value": "Pending", "short": true } ] } ] }
my test apex class
@RestResource(Urlmapping='/v1/slack/*')
global with sharing class SlackRest{
@HttpGet
global static String getRequest() {
RestRequest req = RestContext.request;
system.JSONGenerator gen = JSON.createGenerator(true);
gen.writeStartObject();
gen.writeStringField('text', 'sample text');
gen.writeEndObject();
String genString = gen.getAsString();
genString = genString.replace('\\"', '"');
genString = genString.replace('"{', '{');
genString = genString.replace('}"', '}');
return genString;
}
}