Home > Net >  Passing JSON as a string in the body of the POST request
Passing JSON as a string in the body of the POST request

Time:08-26

I need a small help, because I don't know how to solve the below problem.

The requirement is simple, I have to sent the JSON to the server as a string parameter. The server basing on the key finds the mapping, and generically parses the JSON to some objects. That means, that the payload can have a different values and structures, each key has its own mapping - different data structure, number of parameters and so on. So the payload shouldn't be parsed outside the endpoint logic.

I know, that the Swagger sees the payload as a JSON, not as a string, and it tries to parse the data. How can I send the JSON as a string parameter to the endpooint without parsing the parameter? I have to parse it inside of the application, because of the mentioned mappings.

Example JSON:

{
  "key": "test",
  "payload": "[{"IDNew":1,"NameNew":"t1","DescriptionNew":"t1d", "IntegerValueNew":1, "DecimalValueNew":123.3}]"
}

When I'm trying to send the data in Swagger, I'm getting the below results:

curl -X POST "http://localhost:5110/api/InboundData" -H  "accept: */*" -H  "Content-Type: application/json-patch json" -d "{  \"key\": \"test\", \"payload\": \"[{\"IDNew\":1,\"NameNew\":\"t1\",\"DescriptionNew\":\"t1d\", \"IntegerValueNew\":1, \"DecimalValueNew\":123.3}]\"}"

{
  "errors": {
    "payload": [
      "After parsing a value an unexpected character was encountered: I. Path 'payload', line 3, position 17."
    ]
  },
  "type": "https://tools.ietf.org/html/rfc7231#section-6.5.1",
  "title": "One or more validation errors occurred.",
  "status": 400,
  "traceId": "|d952c89f-4e25126d8cdf3697."
}

Data model:

[Required]
[JsonProperty(Required = Required.DisallowNull)]
[MaxLength(100)]
public string Key { get; set; }
       
[Required]
[JsonProperty(Required = Required.DisallowNull)]
public string Payload { get; set; }

CodePudding user response:

The error clearly suggests that your JSON is not correct. If we analyze the payload property:

{
  "key": "test",
  "payload": "[{"IDNew":1,"NameNew":"t1","DescriptionNew":"t1d", "IntegerValueNew":1, "DecimalValueNew":123.3}]"
}

It seems you are creating a string object which further contains a JSON as a string. Generally, when you pass an array, you would pass it like this.

{
  "key": "test",
  "payload": [
    {
      "IDNew": 1,
      "NameNew": "t1",
      "DescriptionNew": "t1d",
      "IntegerValueNew": 1,
      "DecimalValueNew": 123.3
    }
  ]
}

But, since value of the payload property is not properly escaped, which is why it is not properly able to parse it as it has unexpected characters for a string value.

If you strictly want to pass a JSON Array as a string object, you need to properly escape it in order to get it working:

{
  "key": "test",
  "payload": "[{\"IDNew\":1,\"NameNew\":\"t1\",\"DescriptionNew\":\"t1d\", \"IntegerValueNew\":1, \"DecimalValueNew\":123.3}]"
}

This is how you would escape your JSON if you strictly want to pass a JSON object that further contains JSON as string.

Or, perhaps, use single quote (') instead for the nested JSON:

{
  "key": "test",
  "payload": "[{'IDNew':1,'NameNew':'t1','DescriptionNew':'t1d', 'IntegerValueNew':1, 'DecimalValueNew':123.3}]"
}
  • Related