0

I have created an rest api in salesforce and can be accessed using site.com with the class url, while accessing the rest api in browser I am getting response as

<response>
[{"attributes":{"type":"Account","url":"/services/data/v37.0/sobjects/Account/0012800000hij6DAA Q"},"Name":"GenePoint","Id":"0012800000hij6DAAQ"},{"attributes":{"type":"Account","url":"/services/data/v37.0/sobjects/Account/0012800000hij6BAAQ"},"Name":"United Oil & Gas, UK","Id":"0012800000hij6BAAQ"},{"attributes":{"type":"Account","url":"/services/data/v37.0/sobjects/Account/0012800000hij6CAAQ"},"Name":"United Oil & Gas, Singapore","Id":"0012800000hij6CAAQ"},{"attributes":{"type":"Account","url":"/services/data/v37.0/sobjects/Account/0012800000hij63AAA"},"Name":"Edge Communications","Id":"0012800000hij63AAA"},{"attributes":{"type":"Account","url":"/services/data/v37.0/sobjects/Account/0012800000hij64AAA"},"Name":"Burlington Textiles Corp of America","Id":"0012800000hij64AAA"}]
</response>

The information json is in tag. How to avoid the

My Code :

@RestResource(urlMapping='/mycall/*')
global class mycallclass{
@HttpGet
global static String getmycall() {
    Map<String, String> params = RestContext.request.params;
    String name = params.get('name');
    System.debug('param value :: '+name);
    List<Account> acc = [Select name From Account Limit 5];  
    System.debug('Accoount test ' + JSON.serialize(acc));      
    return JSON.serialize(acc);
}
}

Can anyone please help me to fix this issue.

1 Answer 1

0

What is the content-type header you are setting while making the callout? I would suggest you to set your Content-Type in Postman or any client as "application/json" and it will return data as JSON.

Either way you can force your code to return JSON using the below code.

 @RestResource(urlMapping='/mycall/*')
global class mycallclass{
@HttpGet
global static void getmycall() {
    Map<String, String> params = RestContext.request.params;
    String name = params.get('name');
    System.debug('param value :: '+name);
    List<Account> acc = [Select name From Account Limit 5];  
    RestContext.response.addHeader('Content-Type', 'application/json');
     RestContext.response.responseBody = 
        Blob.valueOf(JSON.serialize((List<Account>)[SELECT Name, Id FROM Account LIMIT 5]));



}
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .