RESTAPIResponseStream - writeString(String data)

Write string data to the response stream.

You must set the content type and status code before calling the writeString() method or the response will fail. You cannot modify these values after calling the writeString() method.

Table 1. Parameters
Name Type Description
data String The string to add to the response data.
Table 2. Returns
Type Description
void

response.setContentType('application/json');
response.setStatus(200);
var writer = response.getStreamWriter();
var body ={
  name:user1,
  id: 1234,
  roles: [
    {
      name: admin
    },
    {
      name: itil
    }
  ]
}
writer.writeString("{'name':'user','id':'1234'}");
writer.writeString(JSON.stringify(body));