Solved

post to REST service with json array

  • 6 March 2020
  • 3 replies
  • 24 views

hi,

I have a REST service which needs an json array as an input. Like:

{
"array" :
[
{
"value1" : "abc",
"value2": 1.00
},
{
"value1" : "def",
"value2": 2.00
}
]
}

The SmartObjects method gets an Memo input parameter (requestarray). I think I have to expose this input parameter as a list with the serialize list function of the requestarray.

I know how to deserialize response values, but I couldn't figure out how to do it on the input side.

 

Is there a tutorial somewhere?

 

icon

Best answer by reto 16 March 2020, 07:01

View original

3 replies

Hi reto,


 


Perhaps you will find our documentation on REST Swagger file reference formats and Serialization/Deserialization to be helpful:


 


https://help.k2.com/onlinehelp/K2Five/UserGuide/5.3/default.htm#ServiceBrokers/EndPoints/REST_SwaggerFileReferenceFormat.htm


https://help.k2.com/onlinehelp/K2Five/DevRef/5.3/default.htm#SBrokers/SB-Serialization.htm


 


Thanks


 


 

I ended up building the requestarray as a string with a formula. Works fine.

But I still wonder if there's a better solution.

This is an old question, but the information might be helpful for others.





You may need to modify your swagger definition file. In many cases, Swagger creates files that will define sub-objects inline within their parent objects (or even directly within the endpoint), and K2 doesn't like these. For example, Swagger might generate a file with these definitions:



"definitions": {

"RequestBody": {

"type": "object",

"properties": {

"array": {

"type": "array",

"items": {

"type": "object",

"properties": {

"value1": {

"type": "string"

},

"value2": {

"type": "number",

"format": "double"

}

}

}

}

}

}

}




This definition would give you a RequestBody object that can be serialized or deserialized, but it wouldn't give any way to serialize or deserialize the "array" property.





Instead, each "object" type needs to have its own definition, and the parent object needs to have a reference to the sub-object type. This gives the sub-object type a name, which you can then use to find the SmartBroker methods to serialize / serialize to array / serialize add to array.





The following definitions section is functionally identical to the above, but it would create a RequestBodyArray object in the SmartBroker, allowing you to serialize your array without resorting to direct string manipulation:



"definitions": {

"RequestBody": {

"type": "object",

"properties": {

"array": {

"type": "array",

"items": {

"$ref": "#/definitions/RequestBodyArray"

}

}

}

},

"RequestBodyArray": {

"type": "object",

"properties": {

"value1": {

"type": "string"

},

"value2": {

"type": "number",

"format": "double"

}

}

}

}


Reply