HTTP Plugin to invoke Rest webservice

Questions and answers on designing your Servoy solutions, database modelling and other 'how do I do this' that don't fit in any of the other categories

HTTP Plugin to invoke Rest webservice

Postby dfernandez » Sat Mar 19, 2016 8:29 pm

Hi,

I need to invoke a rest webservice to get the name of a product. How shoud I do with http plugin?

Example query
http://api.ean-search.org/api?token=abc ... 9750442227

Example response
<?xml version="1.0" encoding="UTF-8"?>
<BarcodeLookupResponse>
<product>
<ean>5099750442227</ean>
<name>Michael Jackson - Thriller</name>
</product>
</BarcodeLookupResponse>

In case the barcode can not be found, the API returns
<?xml version="1.0" encoding="UTF-8"?>
<Error>Barcode not found</Error
dfernandez
 
Posts: 87
Joined: Wed Feb 29, 2012 4:04 pm

Re: HTTP Plugin to invoke Rest webservice

Postby marco.rossi » Mon Mar 21, 2016 1:15 pm

Hi Fernandez,

here an example to create a request on a rest webservice:

Code: Select all
        //Build here the webservice url
   var urlToYourRestWS = "http://api.ean-search.org/api?token=abcdef&op=barcode-lookup&ean=5099750442227"
   var client = plugins.http.createNewHttpClient() //create a new client
   var request = client.createGetRequest(urlToYourRestWS) //create a request
   var response = request.executeRequest() //execute the request
   var body = request.getResponseBody() //read result



Hope this help

Marco Rossi
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: HTTP Plugin to invoke Rest webservice

Postby dfernandez » Tue Mar 22, 2016 12:22 am

Hi Marco,

Thank you!

Function getResponseBody is not there:

var body = request.getResponseBody() //read result

TypeError: Cannot find function getResponseBody in object com.servoy.extensions.plugins.http.GetRequest@5bc40663. (C:\Tests\servoy8\servoy_workspace8a\LlamarWsEAN\forms\LlamarWS.js#28)

Regards
dfernandez
 
Posts: 87
Joined: Wed Feb 29, 2012 4:04 pm

Re: HTTP Plugin to invoke Rest webservice

Postby dfernandez » Tue Mar 22, 2016 12:32 am

Hi Marco,

The problem was in the last line. It shoud be var body = response.getResponseBody() //read result
It works fine now. Thank you very much.

Now I need to parse the result to take the name.

Regards
dfernandez
 
Posts: 87
Joined: Wed Feb 29, 2012 4:04 pm

Re: HTTP Plugin to invoke Rest webservice

Postby juan.cristobo » Tue Mar 22, 2016 10:25 am

If the response is in JSON format you can convert it to an object:

Code: Select all
var oResp = JSON.parse(body)
Juan
Madrid (Spain)

Servoy 7.4.x - MySQL / SQL Server 2008-2016
Windows 10 Pro
juan.cristobo
 
Posts: 186
Joined: Thu Apr 19, 2012 9:12 am

Re: HTTP Plugin to invoke Rest webservice

Postby marco.rossi » Tue Mar 22, 2016 11:25 am

Hi Fernandez,

yes sorry, I've wrote the code directly here within the forum.

Glad to be of assistance.
Marco Rossi
Senior Analyst Developer
Freelance Consultant

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


Return to Programming with Servoy

Who is online

Users browsing this forum: No registered users and 8 guests