4

Consider the following class:

public class Caps {
    public Sett SETTINGS {get;set;}
}
public class Sett {
    public string foo {get;set;}
}

When I POST the following JSON:

{"SETTINGS":{"foo":"bar"}}

To my own REST class:

...
@HttpPost
global static void doPost(Caps change) {
}

I get 400 Bad Request:

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

However the exact same JSON can be de-serialised as follows:

String s = '{"SETTINGS":{"foo":"bar"}}';
Caps c = (Caps)JSON.deserialize(s, Caps.class);
system.debug(c);

As a workaround I can do the de-serialisation in my REST class:

@HttpPost
global static ZRest.ZRestResponse doPost() {
    String req = RestContext.request.requestBody.toString();
    Caps change = (Caps)JSON.deserialize(req, Caps.class);
}

This works fine and I get 200.

The question is why doesn't the standard REST de-serialisation of parameters work?

PS: I read possible duplicate questions but they only seem to offer a workaround or the same string in a different format (newlines, indents) which doesn't solve my issue at all.

7

This is a consequence of Salesforce's design decision that allows for example this code:

@HttpPost
global static void doPost(String x, Integer y) {
}

to accept this JSON:

{
    "x": "abc",
    "y": 123
}

The names and types of the method arguments form part of the JSON structure.

So in your case the following JSON would work:

{"change":{"SETTINGS":{"foo":"bar"}}}
5
  • Your first sentence seems like it's either ungrammatical or missing a link. Still, +1, this is good to know.
    – Adrian Larson
    Feb 22 '16 at 16:59
  • @AdrianLarson I've added a few more words to try to improve.
    – Keith C
    Feb 22 '16 at 17:21
  • Much less confusing for me! I made one more minor tweak, hope you don't mind.
    – Adrian Larson
    Feb 22 '16 at 17:23
  • @AdrianLarson No problem.
    – Keith C
    Feb 22 '16 at 17:25
  • It would be icing on the cake if you can find a supporting documentation link.
    – Adrian Larson
    Feb 22 '16 at 17:27

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.