A return value of 500 means that your server encountered an unusual condition (a script or environment error). This means, most likely, that your data is incorrect. I suspect that the 500 error occurred because of a lack of data; your POST body is supposed to be 10,240 bytes, and you are sending far less than that, which is probably why the connection fails.
The basic function looks like this:
This is basically outlined in the relevant section in the Apex Code Developer's Guide, but isn't specifically spelled out that way.
HttpRequest req = new HttpRequest();
Http http = new Http();
HttpResponse resp = new HttpResponse();
String Uname='raviaruva';
String pwd='raviaruva';
req.setEndpoint('http://xyz/BMEapp/API.asmx/DataSink');
req.setBody('username='+EncodingUtil.urlEncode(Uname, 'UTF-8')
+'&password='+EncodingUtil.urlEncode(pwd, 'UTF-8')
+'&info='+EncodingUtil.urlEncode(data, 'UTF-8'));
req.setMethod('POST');
req.setHeader('content-type', 'text');
req.setHeader('Content-Length','10240');
printUrl = req.toString();
req.setCompressed(true);
// add the endpoint to the request
try {
resp = http.send(req);
} catch(System.CalloutException e) {
System.debug('Callout error: '+ e);
System.debug(resp.toString());
}
Its giving 500 Error
What is the problem
HttpRequest req = new HttpRequest();
Http http = new Http();
HttpResponse resp = new HttpResponse();
String Uname='raviaruva';
String pwd='raviaruva';
req.setEndpoint('http://xyz/BMEapp/API.asmx/DataSink');
req.setBody('username='+EncodingUtil.urlEncode(Uname, 'UTF-8')
+'&password='+EncodingUtil.urlEncode(pwd, 'UTF-8')
+'&info='+EncodingUtil.urlEncode(data, 'UTF-8'));
req.setMethod('POST');
req.setHeader('content-type', 'text');
req.setHeader('Content-Length','10240');
printUrl = req.toString();
req.setCompressed(true);
// add the endpoint to the request
try {
resp = http.send(req);
} catch(System.CalloutException e) {
System.debug('Callout error: '+ e);
System.debug(resp.toString());
}
Its giving 500 Error
What is the problem
A return value of 500 means that your server encountered an unusual condition (a script or environment error). This means, most likely, that your data is incorrect. I suspect that the 500 error occurred because of a lack of data; your POST body is supposed to be 10,240 bytes, and you are sending far less than that, which is probably why the connection fails.
Hello,
Can you please send me the working sample code .
Regards.
Praveen Gaddam
Hi All,
This is the solution
String strUsername='praveen';
String strPwd='praveen';
Http m_http = new Http();
HttpRequest req = new HttpRequest();
String content = 'username='+EncodingUtil.urlEncode(strUsername, 'UTF-8')+'&password='+EncodingUtil.urlEncode(strPwd, 'UTF-8')+'&listname='+EncodingUtil.urlEncode('PraveenKumarG', 'UTF-8');
req.setEndpoint('http://xyz/BMEapp/API.asmx/listCreate');
req.setHeader('Content-Type','application/x-www-form-urlencoded');
req.setMethod('POST');
req.setBody(content);
httpResponse response = m_http.send(req);
Regards,
Praveen Gaddam
req.setEndpoint(https://alivecoupon.com);
req.setMethod('GET');
Http http = new Http();
HttpResponse res = http.send(req);
String body = res.getBody();
system.debug('Our response: ' + body);
@RestResource(urlmapping = '/sendComment/*')
global without sharing class postComment {
@HttpPost
global static void postComment(){
//create parameters
string commentTitle = RestContext.request.params.get('commentTitle');
string textBody = RestContext.request.params.get('textBody');
//equate the parameters with the respective fields of the new record
Comment__c thisComment = new Comment__c(
Title__c = commentTitle,
TextBody__c = textBody,
);
insert thisComment;
RestContext.response.responseBody = blob.valueOf('[{"Comment Id":
'+JSON.serialize(thisComment.Id)+', "Message" : "Comment submitted
successfully"}]');
}
}
The URL for the above API class will look like -
/services/apexrest/sendComment?commentTitle=Sample title&textBody=This is a comment