function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
SahajSahaj 

Receiving 200 response code,instead of 201 for record insertion.

Hi All, 

I am recieving 200 response code,instead of 201 for record insertion. Can you please help. PFB code and debug logs.
Thanks alot..
Global class Moogsoft {
    @future (callout=true)
    public static void getMoog(Integer inc, Integer moog) {
        String authT = authToken();
        system.debug('####'+authT);
        String firstC = firstCall(inc,moog,authT);
        system.debug('####'+firstC);
        String secondC = secondCall(inc,moog,authT);
        system.debug('####'+secondC);
    }

    public static string authToken() {
        String URL1 = 'https://events5.mop.com:8080/graze/v1/authenticate?username=graze&password=graze';
        HttpResponse resData = HTTPCallout(URL1, 'GET');
        System.debug('Response from Moog: ('+resData.getStatusCode()+')'+resData.getBody());

        String authToken;

        if(resData.getStatusCode()>299) {
            String error = 'Failed getting a request token. HTTP Code = '+resData.getStatusCode()+
                            '. Message: '+resData.getStatus()+'. Response Body: '+resData.getBody();
            system.debug('failed'+error);
        } else {
            JSONParser parser = JSON.createParser(resData.getBody());
            while (parser.nextToken() != null) {
                if (parser.getCurrentToken() == JSONToken.FIELD_NAME){
                    String fieldName = parser.getText();
                    parser.nextToken();

                    if(fieldName == 'auth_token' ) {
                        authToken = parser.getText();
                        system.debug('++++++++++'+authToken);
                    }
                }
            }
        }

        return authtoken;
    }

    public static string firstCall(Integer a1,Integer b1,String c1) {
        Integer aa = a1;
        Integer bb = b1;
        String cc = c1;
        System.debug('####'+cc);
        String URL2 = 'https://events5.mop.com:8080/graze/v1/addSigCorrelationInfo?auth_token='+cc+'&sitn_id='+bb+'&service_name=Remedyforce&resource_id='+aa+'';
        HttpResponse res = HTTPCallout(URL2, 'POST');
        System.debug('Response from Code request: ('+res.getStatusCode()+')'+res.getBody());

        if(res.getStatusCode()>299) {
            String error = 'Request failed error.HTTP Code = '+res.getStatusCode()+
                        '. Message: '+res.getStatus()+'. Response Body: '+res.getBody();
            System.debug('##### Failed: '+error);
            return error;
        }
            
        String suc ='successfull';
        return suc;
    }

    public static string secondCall(Integer a2,Integer b2,String c2) {
        Integer aa = a2;
        Integer bb = b2;
        String cc =  c2;
        String URL3 = 'https://ev.mop.com:8080/graze/v1/addSituationCustomInfo?auth_token='+cc+'&sitn_id='+bb+'&custom_info={incrf:'+aa+'}';
        HttpResponse resT = HTTPCallout(URL3, 'POST');
        System.debug('Response from Code request: ('+resT.getStatusCode()+')'+resT.getBody());

        if(resT.getStatusCode()>299) {
            String error = 'Request failed error.HTTP Code = '+resT.getStatusCode()+
                        '. Message: '+resT.getStatus()+'. Response Body: '+resT.getBody();
            System.debug('##### Failed: '+error);
            return error;
        }

        String suc ='successfull';
        return suc;
    }

    public Static HttpResponse HTTPCallout(String EndPoint, String Method) {
        Http h = new Http();
        HttpRequest req= new HttpRequest();
        req.setEndpoint(EndPoint);
        req.setMethod(Method);
        HttpResponse res = null;
        res = h.send(req);
        return res;
    }
}

User-added image
Best Answer chosen by Sahaj
James LoghryJames Loghry
This depends on your external service, not Salesforce.  Depending on what request you're sending and under which conditions, the service will send back what it thinks is the appropriate response.  200 is a successful response, so is 201.  Although they mean different things technically, they both indicate succesfull responses, but the implementor decided to return a 200 instead of a 201.

I would contact the provider of https://events5.mop.com:8080/graze/v1/addSigCorrelationInfo if you have any further questions on why it would be returning 200 instead of 201.

All Answers

James LoghryJames Loghry
This depends on your external service, not Salesforce.  Depending on what request you're sending and under which conditions, the service will send back what it thinks is the appropriate response.  200 is a successful response, so is 201.  Although they mean different things technically, they both indicate succesfull responses, but the implementor decided to return a 200 instead of a 201.

I would contact the provider of https://events5.mop.com:8080/graze/v1/addSigCorrelationInfo if you have any further questions on why it would be returning 200 instead of 201.
This was selected as the best answer
SahajSahaj
Thanks alot James..