RESTful API, advanced body parameters

RESTful API, advanced body parameters

  

Hi favorite forum,

I am consuming a local API to my web application on premises and I have a problem with a body parameter. This particular body parameter is formatted like JSon, however it is just text at top level. When I made my REST POST method it worked fine just by writing it into the field like:

However, since the body parameters are URL-encoded I can not use the same text in the parameter input when I POST at runtime and the text editor I am provided does not like my strings.

I think I have to use some kind of structure for workaround but from there I'm stuck, so I'm asking you guys for some guidance.

Thanks in advance! 

Hi Jack,

It seems your body is actually Form URL Encoded, a format that the OutSystems Platform supports. You need to set the Request Format of the consumed REST service to "Form URL Encoded", and define two parameters (or more, in case the image doesn't show all) for the "region" and "report" respectively (both having their "Send In" Property set to "Body"). Note you can give these parameters different names if so desired (e.g. "Region" and "ReportJson" or whatever), as long as you set the "Name in Request" Property to the actual names).

To get the JSON for the "report", you can handcraft a structure for it, and use the JSON Serialize Statement to get the JSON. Alternatively, if it's a complex structure, you could trick Service Studio to create the Structures for you by first setting the Request Format to "JSON", then pasting the JSON into the Request area of the Body tab (in the REST consume pop-up) and pressing Ok (and afterwards setting the Request Format to "Form URL Encoded" again).

Thank for your answer Kilian!

I have used URL encoded all along, the problem was that the "JSon" (which actually is just a text that looks like JSon to be handed over to another request by the server, where it later is implemented as JSon) was implemented as URL encoded.  When I logged the request the symbols came out percentage formatted. 

If I use JSon serialize instead, will the request be read as intended then? I tried to solve it using structures but I can't see if there is a way to create structures at runtime if they do not come from a action or REST method. Any tips?

Hi Jack,

Using a URL encoded body is indeed "percentage formatting" the request. If that's not what the consumed REST API can handle, you need to "manually" encode the request. Use a single Input Parameter with "Send In" set to "Body", and set the Method's "Request Format" to "Plain Text". Next, set the Input Parameter to the desired format, e.g.

"region=1&report=" + JSONSerialize1.JSON

Where JSONSerialize1 is the serialized JSON.

As for the Structures, I'm not sure I understand what you mean by "creat[ing] structures at runtime". I assume the JSON that is in the report parameter is not dynamic, i.e. it can have one or more incarnations, but it can't be just anything? In that case, I already described above how to handle that (i.e. creating the structures).

Oh okey I see, I will try that aswell thanks. "Creating structures at runtime" was a strange formulation by me I'm sorry about that. I feel like I'm missing something here and asking too much for help, but the thing I can't seem to understand is a nice way of creating an object of the structure that I made, so that I can use it.

I have a structure that fits the specification, but to serialize it with data assigned to it I need to create an object off it somehow, right?

I'm very grateful for you guys answering all these questions..

Solution

Hi Jack,

If you already have the structure, creating JSON from it is as simple as using the JSON Serialize Statement*:

If you drag it onto the canvas, specify the Structure in the Data Property. The Output can be fetched via the JSON Output Parameter.

*"Statement" is OutSystems parlance for the built-in tools and actions in the toolbar, like "Aggregate", "For Each" and so on. So you can find the JSON Serialize there.


Solution