You need to sign in to do that
Don't have an account?
Ranjan 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
}
@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
}
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)
Hi ,
check with res.getStatusCode==200 like
if(res.getStatusCode() == 200){
}
then it will work fine once it gets the statuscode as 200.