You need to sign in to do that
Don't have an account?
bdardin
RestRequest body not showing urlencoded data
I am working on an integration with an external application that sends us urlencoded data (content type "application/x-www-form-urlencoded"). I created a POST method without any parameters. According to the documentation, the request should then be loaded into the requestBody property of the RestRequest object. However when I'm testing it, the debug log shows the value of the blob to be "Blob[0]", and converting the blob to a string shows nothing (as in, it's blank, it doesn't say "null"), nor does trying to decode that string. The request isn't being passed into the paramters as all the parameter values are null (as in, they show as "null", they're not blank). The entire class is below, all I'm doing at the moment is logging everything in the Request. What am I doing wrong? How do I make the request show in the blob?
@RestResource(urlMapping='/servicechannel/*')
global without sharing class ServiceChannelWS {
@HttpPost
global static void doPost() {
RestRequest req = RestContext.request;
Map<String, String> headers = req.headers;
system.debug('Headers');
for(String s : headers.keySet()) {
system.debug('Key: ' + s + ' Value: ' + headers.get(s));
}
system.debug('Method: ' + req.httpMethod);
Map <String, String> params = req.params;
if(params != null) {
system.debug('Params');
for(String s : headers.keySet()) {
system.debug('Key: ' + s + ' Value: ' + params.get(s));
}
}
system.debug('IP Address: ' + req.remoteAddress);
Blob b = req.requestBody;
if(b != null) {
system.debug('Body Blob: ' + b);
String body = b.toString();
system.debug('Body String: ' + body);
String msg = EncodingUtil.urlDecode(body, 'UTF-16');
system.debug('Body Decoded: ' + msg);
}
system.debug('Request URL: ' + req.requestURI);
}
}
@RestResource(urlMapping='/servicechannel/*')
global without sharing class ServiceChannelWS {
@HttpPost
global static void doPost() {
RestRequest req = RestContext.request;
Map<String, String> headers = req.headers;
system.debug('Headers');
for(String s : headers.keySet()) {
system.debug('Key: ' + s + ' Value: ' + headers.get(s));
}
system.debug('Method: ' + req.httpMethod);
Map <String, String> params = req.params;
if(params != null) {
system.debug('Params');
for(String s : headers.keySet()) {
system.debug('Key: ' + s + ' Value: ' + params.get(s));
}
}
system.debug('IP Address: ' + req.remoteAddress);
Blob b = req.requestBody;
if(b != null) {
system.debug('Body Blob: ' + b);
String body = b.toString();
system.debug('Body String: ' + body);
String msg = EncodingUtil.urlDecode(body, 'UTF-16');
system.debug('Body Decoded: ' + msg);
}
system.debug('Request URL: ' + req.requestURI);
}
}
Code is corrent check your client side.
This is the method I'm using to test:
@Future(callout=true)
public static void sendRequest(String xml){
String start = 'msg='+xml;
String encoded = EncodingUtil.urlEncode(start, 'UTF-16');
HttpRequest req = new HttpRequest();
req.setEndpoint(endpointURL); //removed URL from example so it can't be used by others
req.setMethod('POST');
req.setBody(encoded);
req.setHeader('Content-Type', 'application/x-www-form-urlencoded');
req.setTimeout(60000);
Http http = new Http();
if(!test.isRunningTest()){
HTTPResponse res = http.send(req);
system.debug('Status: ' + res.getStatus() + ' Status Code: ' + res.getStatusCode() + ' Body: ' + res.getBody());
}
}
Did you ever get any resolution for this?
I am trying to do the same thing and was wondering if it was possible?
Thanks
Jyoti