Apply mapping as JSON on puplished rest service - Mendix Forum

Apply mapping as JSON on puplished rest service

13

When publishing a rest service, it is possible to use a microflow returning the object (or list of objects) that should build the body of a response. Now you can directly apply an export mapping. That's great.
Unfortunately, this always creates XML. In order to get a JSON response, you need to apply the mapping within your microflow and return a string. As I am pretty sure that it is using the same mapping mechanisms, it would be great to have the XML/JSON selection available when using the export mapping directly on the published rest service configuration, instead of doing it in a microflow action.

Thanks

asked
8 answers

Adding to @Andrej Koelewijn's remark: If the Accept header in the http request contains the value 'application/json', then Mendix will do the rest automatically. Likewise, it can also be changed back to XML with the value 'application/xml'

Created

The mean idea with REST is that the client can specify what format is easiest for him. The client can do this by specifying the content-type in the request. If the client requests xml mendix will generate xml, if the client requests json, mendix will generate json. Ideally you don't want to enforce a specific format in the server.

Created

You don’t need an export with mapping activity. You can return the object(s) in the microflow, and specify which mapping to apply in the published REST operation.

If you use an export mapping based on a message definition, it can produce both XML and JSON. You choose which one you want by adding a Content-Type header to the HttpResponse.

If I understand correctly, Andreas’ idea is to add an option in the published REST operation to specify whether you want XML or JSON. That’s a nicer way of specifying it.

Created

Why is it so much more complicated to just apply the export mapping as JSON? It should a selectable option somewhere, right? Using the “Export with Mapping” activity and creating http responses and headers manually seems to be more like a workaround?

Created

What is your export mapping based on? WSDL, XSD, JSON structure or message definition?

Created

@Joost we tried to do that, it did not work. But maybe we did something wrong and should try again. Thanks.

Created

Have you tried adding a System.HttpResponse parameter to your microflow, and adding a Content-Type header with value application/json? That way you indicate that you want JSON output.

See section 2.7.2 Export Mapping in https://docs.mendix.com/refguide/published-rest-operation

 

Created

This is a great idea! This would help a lot!

Created