0

I'm writing a rest api , and trying to return a custom response (i.e a Map<String,Object>) , but salesforce is giving me HttpPatch methods do not support return type of System.RestResponse so i decided to populate RestContext.response but when i run it through an postman , i get 200 response all the time , an empty body , event thought i did send a body with the error code?

How can i fix this error and see the proper response?

code :

  @HttpPatch
    global static void doPost(String accountid) 
    {
        Account myaccount;
        list <Opportunity> myresult;
        try
        {
            myaccount = [select website from Account where id=:accountid];
            myresult=[select id from Opportunity where website__c=:myaccount.website limit 10];

            RestContext.response.addHeader('Content-Type', 'application/json');
            RestContext.response.responseBody = Blob.valueOf(JSON.serialize(myresult));
    response.statusCode = 201;
        }
        catch (Exception e) 
        {
            RestContext.response.addHeader('Content-Type', 'text/plain');
            RestContext.response.responseBody = Blob.valueOf('Incorrect account id');
    response.statusCode = 400;
        }
    }
2
  • 1
    There are several incosistencies between what you say and what your code says (you say Patch, your code says Post; you say you get HTTP 200, your code says you return HTTP 201). Are you sure you're looking at the correct code? Are you sure you're making the correct call with Postman?
    – Derek F
    Dec 6, 2021 at 15:29
  • Post to Patch is a genuine one (corrected it) , but it should send 201 but it's sending 200 all the time , i thought that was very clear...
    – Akash Jain
    Dec 7, 2021 at 6:14

1 Answer 1

1

I remember I had a similar problem a while ago.

Try setting the RestContext.response to a variable, and setting your response status code and body on that variable.

Change your code to the following:

@HttpPost
    global static void doPost(String accountid) 
    {
        Account myaccount;
        list <Opportunity> myresult;
        RestResponse res = RestContext.response;
        try
        {
            myaccount = [select website from Account where id=:accountid];
            myresult=[select id from Opportunity where website__c=:myaccount.website limit 10];

            res.addHeader('Content-Type', 'application/json');
            res.responseBody = Blob.valueOf(JSON.serialize(myresult));
            res.statusCode = 201;
        }
        catch (Exception e) 
        {
            res.addHeader('Content-Type', 'text/plain');
            res.responseBody = Blob.valueOf('Incorrect account id');
            res.statusCode = 400;
        }
    }
2
  • doing this is giving me null value for response...
    – Akash Jain
    Dec 7, 2021 at 6:31
  • ignore that, it's working fine thanks..
    – Akash Jain
    Dec 7, 2021 at 6:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.