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
Ranjan Kumar 20Ranjan Kumar 20 

workbench rest explorer is showing 200 ok status but in debug log in am getting 400 bad request error . and the same code is working sometimes

Here is my code,

@RestResource (urlMapping='/rateupdate/test/*')
global class SampleRESTService {
    @HttpPost
    global static void doPost(){
        
        
        RestRequest req = RestContext.request;
        
        Map<String, Object> body = new Map<String, Object>(); 
        String cur = '';
        Double rate = 0.00;
        String id;
        String jsonBody = req.requestBody.toString();
        
        if (!String.isBlank(jsonBody)) {
            body = (Map<String, Object>)JSON.deserializeUntyped(jsonBody);
            if (body.containsKey('currency')) {
                cur = (String)body.get('currency');
                CurrencyType ct =  [Select id,IsoCode,ConversionRate From CurrencyType where isActive=true And IsoCode=:cur];
                system.debug('currencyType====='+ct);
                id = ct.Id;
            }
            
            if (body.containsKey('rate')) {
                rate = (Double)body.get('rate');
             }
        
        
        }
   
        Http h = new Http();
        HttpRequest req1 = new HttpRequest();
        req1.setEndpoint(URL.getSalesforceBaseUrl().toExternalForm() + '/services/data/v28.0/sobjects/CurrencyType/'+id+'?_HttpMethod=PATCH');
        
        Map<String,double> rateMap =new Map<String,double>();
        
        rateMap.put('ConversionRate',rate);
        
        String body1=JSON.serialize(rateMap);
        system.debug('body==='+JSON.serialize(rateMap));
        
        req1.setBody(body1);  
        req1.setHeader('Authorization', 'OAuth ' + UserInfo.getSessionId());
        req1.setHeader('Content-Type', 'application/json');
        req1.setMethod('POST');
        HttpResponse res = h.send(req1);
        
        system.debug('response====='+res);
     
    }

}

Json i am posting from workbench:;

{
    "currency": "USD", 
    "rate": 9.00
                
}
kathiravan R 1kathiravan R 1

Hi Ranjan,

 Http code error: 400 is because of bad request from client side.
Http code error: 200 is for success alert.

so check your entry criteria as if(criteria == 200) then do further operations .

if you try like this your code will work fine.

for your reference about http code errror : Please go through the below link you will get much clarify about http codes.

https://www.restapitutorial.com/httpstatuscodes.html  (https://www.restapitutorial.com/httpstatuscodes.html

 

 

kathiravan R 1kathiravan R 1

Hi ,

check with res.getStatusCode==200 like 

 

if(res.getStatusCode() == 200){

}

then it will work fine once it gets the statuscode as 200.