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

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?

global without sharing class ServiceChannelWS {
    global static void doPost() {
        RestRequest req = RestContext.request;
        Map<String, String> headers = req.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) {
            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);
req.requestBody; never goes NULL, it is always "BLOB[0]" an empty blob when nothing is sent. Please make sure that you are sending data in request body or not.

Code is corrent check your client side.
Even when I test the web service through a callout method in SFDC and I set the body myself, I'm still getting the same result. Is there a special way the urlencoded data needs to be sent (other than being encoded of course) in order for it to work? We've only been able to get it to work if we change the content type to XML or JSON

This is the method I'm using to test:

    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.setHeader('Content-Type', 'application/x-www-form-urlencoded');
       Http http = new Http();
           HTTPResponse res = http.send(req);
           system.debug('Status: ' + res.getStatus() + ' Status Code: ' + res.getStatusCode() + ' Body: ' + res.getBody());
Hello bdardin,

Did you ever get any resolution for this?
I am trying to do the same thing and was wondering if it was possible?