+ Start a Discussion
EagerToLearnEagerToLearn 

HTTPPost always fires even when I use the setMethod('GET')

Hi,
I have the coded class below but when I do the http call out using setMethod('GET') or setMethod('POST') the @HTTPPOST (doDeactivate() method) fires, never the @HTTPGET (UserExists()  method). 
 

What am I doing wrong to cause the HTTPGET to never execute in the class?

CALLOUT IN DEBUGGER:
String UserName = 'username_goes_here';
HttpRequest req = new HttpRequest();
req.setEndpoint('callout:Sandbox_Management/services/apexrest/User/' + UserName);
req.setMethod('POST');
//req.setMethod('GET');
Http http = new Http();
req.setHeader('Content-Type', 'application/json;charset=UTF-8');
req.setBody('{"UserName":UserName}');
HTTPResponse res = http.send(req);
System.debug('RETURNED TO CALLER');
System.debug(res.getBody());


CLASS:
@RestResource(urlMapping='/User/*')
global with sharing class DeactivateUser {
    
    @HttpGet
    global static Boolean UserExists() {
        final String METHOD_NAME = 'DeactivateUser.UserExists()';
        System.debug('---DeactivateUser.UserExists()---');
        Boolean ret = false;
        RestRequest req = RestContext.request;
        RestResponse res = RestContext.response;
        String userName = req.requestURI.substring(req.requestURI.lastIndexOf('/')+1);
        System.debug('Username Passed In: ' + userName);
        List<User> userList = [SELECT Id, UserName, isActive 
                               FROM User 
                               WHERE userName = :userName LIMIT 1];
        if (userList.size() > 0) ret = true;        
        return ret;
    }
    
    @HttpPost
    global static String doDeactivate() {
        final String METHOD_NAME = 'DeactivateUser.doDeactivate()';
        System.debug('---' + METHOD_NAME + '---');
        String retVal = METHOD_NAME + ': Unknown Failure!';
        RestRequest req = RestContext.request;
        RestResponse res = RestContext.response;
        String userName = req.requestURI.substring(req.requestURI.lastIndexOf('/')+1);
        System.debug('Username Passed In: ' + userName);
        User user = [SELECT Id, Username, isActive 
                     FROM User 
                     WHERE userName = :userName LIMIT 1];
        if (user != null) {
            user.isActive = false;
            Try{        
                update user;
                retVal = METHOD_NAME + ': Successfully deactivated user: ' + user.Username;
                return retVal;
            } catch (System.exception e) {
                //Return an error back to the caller
                retVal = METHOD_NAME + ': Error trying to update user: ' + user.Username;
                return retVal;
            }
        } else {
            //Could not find the record so return a message that 
            //informs the caller that the record was not found
            
        }
        return retVal;
    } 
}

Best Answer chosen by EagerToLearn
EagerToLearnEagerToLearn
Ok - I figured it out.  commenting out the req.setBody allowed it to accept the GET.  Appearantly, setBody must flip to a POST automatically!  Not sure how it worked for you but not for me until I commented out the setBody.

All Answers

Raj VakatiRaj Vakati
I dnt see any issue it is working as expected ... 
 
String UserName = 'username_goes_here';
HttpRequest req = new HttpRequest();
req.setEndpoint('callout:Sandbox_Management/services/apexrest/User/' + UserName);
req.setMethod('POST');
//req.setMethod('GET');
Http http = new Http();
req.setHeader('Content-Type', 'application/json;charset=UTF-8');
req.setBody('{"UserName":UserName}');
HTTPResponse res = http.send(req);

System.debug('RETURNED TO CALLER');
System.debug(res);

 
EagerToLearnEagerToLearn
Hi,
Are you saying that when you set req.setMethod('GET') in executes this code?  I tried this over 4 times and everytime the only code that fires is the POST code!  Anyone else getting the experience that I am?  Could it be something setup wrong on the Name Credential, OUATH provifer, or Connected App?


 @HttpGet
    global static Boolean UserExists() {
        final String METHOD_NAME = 'DeactivateUser.UserExists()';
        System.debug('---DeactivateUser.UserExists()---');
        Boolean ret = false;
        RestRequest req = RestContext.request;
        RestResponse res = RestContext.response;
        String userName = req.requestURI.substring(req.requestURI.lastIndexOf('/')+1);
        System.debug('Username Passed In: ' + userName);
        List<User> userList = [SELECT Id, UserName, isActive 
                               FROM User 
                               WHERE userName = :userName LIMIT 1];
        if (userList.size() > 0) ret = true;        
        return ret;
    }
EagerToLearnEagerToLearn
Ok -- I commented the POST code out completely and set the call to GET and I get the following error....
I am new to API/Integration stuff in general but this doesn't make sense the the message says POST not allowed but the req.setMethod is set as req.setMethod('GET').

Can someone please explain this so that I can understand.


20:48:25:490 USER_DEBUG [11]|DEBUG|[{"errorCode":"METHOD_NOT_ALLOWED","message":"HTTP Method 'POST' not allowed. Allowed are GET,HEAD"}]
EagerToLearnEagerToLearn
Ok - I figured it out.  commenting out the req.setBody allowed it to accept the GET.  Appearantly, setBody must flip to a POST automatically!  Not sure how it worked for you but not for me until I commented out the setBody.
This was selected as the best answer