1

Before I go ahead and describe my issue, I have looked at a resolution as described below and does not seem to help in my case. is that possible to pass(POST) below JSON to REST endpoint?

So, I have a json object - really long, but let me shorten it as below:

{
    "jsonStr": {
    "myId": "1010101010101010",
    "randomInt": 1,
    "arrayOfObjs": [{
        "objid": "string_data",
        "mytimestamp": "xxxxxxxxxxxx",
        "externalRefId": "1010101010101010",
        "myMap": {
                "key1": "val1",
                "key2": "val2"
            }
        }]
    }
}

I pass this to the post method of my rest resource. The param name is String jsonStr similar to below:

@HttpPost
global static String doPost(String jsonStr) {

An error with status code 400 BAD REQUEST is sent by the salesforce endpoint with the following description:

{
        "message": "Unexpected parameter encountered during deserialization: myId at [line:1, column:XX]",
        "errorCode": "JSON_PARSER_ERROR"
}

I have also tried it without the outer JSON as in below:

{
 "myId": "1010101010101010",
 "randomInt": 1,
 "arrayOfObjs": [{
 "objid": "string_data",
 "mytimestamp": "xxxxxxxxxxxx",
 "externalRefId": "1010101010101010",
 "myMap": {
     "key1": "val1",
     "key2": "val2"
     }
    }]
}

I have tried passing a basic String in place of the JSON and it works perfectly. Unfortunately, I do not know what is wrong since the JSON is correct and I validated it here https://jsonlint.com/.

I have this serialized/de-serialized separately with Apex HTTP callouts as well. However, I get an error even before I reach the deserialization section in the POST operation. It fails to just receive the JSON and tries to internally parse it.

Any help would be much appreciated.

Thanks,

Adarsh

  • I overcame this issue by converting the json parameter to Base64String, decoding it to blob at the endpoint and then proceeding to get the parameter via Blob.toString(). I feel this is a dirty way to overcome this problem and would still like an answer. – Randomizer Aug 14 '18 at 11:17
3

You can do that in a different way without relying on Native parser of SF to parse your string and provide input in your variable jsonStr. When you pass JSON, make your postMethod parameterless. This way your post method apex will be called and SF will not try to parse it. Then you can parse it manually and make your magic work.

public class RequestWrapper{
    public String myId; //1010101010101010
    public Integer randomInt;   //1
    public cls_arrayOfObjs[] arrayOfObjs;
    class cls_arrayOfObjs {
        public String objid;    //string_data
        public String mytimestamp;  //xxxxxxxxxxxx
        public String externalRefId;    //1010101010101010
        public cls_myMap myMap;
    }
    class cls_myMap {
        public String key1; //val1
        public String key2; //val2
    }
    public static RequestWrapper parse(String json){
        return (RequestWrapper) System.JSON.deserialize(json, RequestWrapper.class);
    }

}



@HttpPost
global static void doPost() {
    RestRequest req = RestContext.request;
    Blob body = req.requestBody;
    String requestString = body.toString();
    //Now as you have the request string You can parse it in APex

    RequestWrapper rw = (RequestWrapper)JSON.deserialize(requestString,RequestWrapper.class);

    //Do your magic
}
  • Thanks for the detailed response. I did try this option, But i got a Blob is not a UTF-8 String error. But I thought the error was on Salesforce and did not check further. But now, I did try your approach but with a basic client (Postman) and it works. However, the body sent from my spring client seems to cause some issues and I will check that. But your overall solution works! I posted a comment on my original question at about the same time indicating how I overcame the problem :) – Randomizer Aug 14 '18 at 11:41
  • If it works from {postman, then it should work with Spring client if configured properly, No worries – Pranay Jaiswal Aug 14 '18 at 11:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.