RESTful Web Services return value

The forum to discuss the Headless version of Servoy. Web, Java and Servlet development questions can all be posted here.

RESTful Web Services return value

Postby marco.rossi » Sat Mar 05, 2016 8:19 pm

Hi all,


I'm getting error with RESTful by returning an object with more than 1 property.

ex:

Code: Select all
var _obj = new Object()
     _obj.id = 1
return _obj


works fine, into the response body I get an xml with a key "ID" and a value "1".

Code: Select all
var _obj = new Object()
     _obj.id = 1
     _obj.myName = "Marco"
return _obj


Gives the error shown within the picture attached.
error.png
error.png (12.56 KiB) Viewed 4778 times


Am I missing something?

Regards

Marco
Marco Rossi
Senior Analyst Developer
Freelance Consultant

IT Manager @Mantho
Webmaster @Sitoliquido
marco.rossi
 
Posts: 110
Joined: Sun Apr 12, 2015 9:33 pm

Re: RESTful Web Services return value

Postby Ruben79 » Mon Mar 07, 2016 12:21 am

Your browser is complaining because the result isn't valid xml, because there is no root element.
The xml will be something like <id>1</id><myName>Marco</myName> if you check the page source.

To have a valid xml, wrap your result in a single node, like this:
Code: Select all
var _obj = new Object()
_obj.result = new Object()
_obj.result.id = 1
_obj.result.myName = "Marco"
return _obj

Another tip is to use a more declaritive approach using this literal notation as example:

Code: Select all
var id = 1;
var myName = 'Marco';

var result = {
   result: {
      id: id, myName: myName
   }
}
   
return result;
Ruben de Jong
Stb Software Development
SAN Partner

Stb Software Development - http://www.stb.nl
User avatar
Ruben79
 
Posts: 97
Joined: Wed Apr 18, 2007 12:43 pm


Return to Servoy Headless Client

Who is online

Users browsing this forum: No registered users and 12 guests