REST - get set request body

Post any questions you have about using the Verj.io Studio, including client and server-side programming with Javascript or FPL, and integration with databases, web services etc.

Moderators: Jon, Steve, Ian, Dave

Steve James
Ebase User
Posts: 331
Joined: Mon Mar 10, 2014 8:34 am

REST - get set request body

#1

Postby Steve James » Tue Jun 13, 2017 11:31 am

Hi, I am working with a 3rd party REST service and they have a GET request that requires some data in the request body.

I cannot use a REST web service resource as for a GET the body cannot be populated.

I am using JAVA openConnection but cannot establish how to set a value in the body.

I can get the response code via getResponseCode() if it is not 200 but I have not been able to get the body.

Any ideas?

Thanks
0 x

Steve
Moderator
Moderator
Posts: 414
Joined: Fri Sep 07, 2007 3:44 pm
Location: Sandy, UK
Contact:

#2

Postby Steve » Wed Jun 14, 2017 8:34 am

Hi Steve,

You need to do something like this:

Code: Select all


importPackage(com.ebasetech.xi.api);
importPackage(com.ebasetech.xi.services);
importPackage(java.net);
importPackage(java.io);

var conn;
try {

		var url = new URL("http://localhost:3030/mygetrequest");
		conn = url.openConnection();
		conn.setDoOutput(true);
		conn.setRequestMethod("GET");
		conn.setRequestProperty("Content-Type", "application/json");

		var input = "{\"qty\":100,\"name\":\"iPad 4\"}";

    var os = conn.getOutputStream();
                //write the body
		os.write(input.getBytes());
		os.flush();

    if (conn.getResponseCode() != 200) 
    {
				throw "Failed : HTTP error code : "+conn.getResponseCode();
		}
		//read the response
    var br = new BufferedReader(new InputStreamReader(conn.getInputStream()));

		var output;
		
		while ((output = br.readLine()) != null)
    {
			log(output);
		}
		
}
catch(e)
{
    log(e);
}
finally
{
    if(conn)
    	conn.disconnect();
}
I hope this helps...

Steve
0 x

Steve James
Ebase User
Posts: 331
Joined: Mon Mar 10, 2014 8:34 am

#3

Postby Steve James » Wed Jun 14, 2017 9:11 am

Thanks Steve, I'm getting an error

Code: Select all

TypeError: Cannot find function getBytes in object {"serviceIds":["4c2fe2853f1f4b429b928faabc8b35e6c6a3154ff883163f4149676e4a84df00","df20bd853e13c5b71715f3f332ec304839637ac8b34cdd3c20c9820f01377dd9"]}. (run#43)
ie the string I need to pass into the body.

Thanks
0 x

Steve
Moderator
Moderator
Posts: 414
Joined: Fri Sep 07, 2007 3:44 pm
Location: Sandy, UK
Contact:

#4

Postby Steve » Wed Jun 14, 2017 9:23 am

Steve James wrote:Thanks Steve, I'm getting an error

Code: Select all

TypeError: Cannot find function getBytes in object {"serviceIds":["4c2fe2853f1f4b429b928faabc8b35e6c6a3154ff883163f4149676e4a84df00","df20bd853e13c5b71715f3f332ec304839637ac8b34cdd3c20c9820f01377dd9"]}. (run#43)
ie the string I need to pass into the body.

Thanks
Ah yes, you'll need to create a Java String:

Change :

Code: Select all

var input = "{"qty":100,"name":"iPad 4"}"; 
to

Code: Select all

var input = new java.lang.String("{"qty":100,"name":"iPad 4"}"); 
Steve
0 x

Steve James
Ebase User
Posts: 331
Joined: Mon Mar 10, 2014 8:34 am

#5

Postby Steve James » Wed Jun 14, 2017 9:53 am

Thanks Steve. 500 error now.

I can't work out how to get the response, I can get the getErrorStream but cannot turn it into a string.

Probably easy :-(
0 x

Steve James
Ebase User
Posts: 331
Joined: Mon Mar 10, 2014 8:34 am

#6

Postby Steve James » Wed Jun 14, 2017 10:08 am

Arghh worked it out. The service doesn't return messages consistently.
Sometimes a 500 error comes with a message, other times nothing....

Thanks Steve.
0 x

Steve
Moderator
Moderator
Posts: 414
Joined: Fri Sep 07, 2007 3:44 pm
Location: Sandy, UK
Contact:

#7

Postby Steve » Wed Jun 14, 2017 10:10 am

Hi Steve,

You should be able to do something like:

Code: Select all

        ........
      if (conn.getResponseCode() != 200) 
     { 
            throw "Failed : HTTP error code : "+conn.getResponseCode(); 
      } 
        //read the response
        var in = conn.getInputStream();
        var out = new java.lang.StringBuffer();
        //create a byte array
        var b= java.lang.reflect.Array.newInstance(java.lang.Byte.TYPE, 1024)
        for (var n; (n = in.read(b)) != -1;)
        {
            out.append(new java.lang.String(b, 0, n));
        }
        
         //create a response var
         var response = out.toString();
         log(reponse);
Steve
0 x

Steve
Moderator
Moderator
Posts: 414
Joined: Fri Sep 07, 2007 3:44 pm
Location: Sandy, UK
Contact:

#8

Postby Steve » Wed Jun 14, 2017 10:16 am

A response can have a response body regardless of the response code.

An error 500 should contain the error message. This usually means that there has been an internal error.

I'm glad you have got it working.

Steve
0 x

Steve
Moderator
Moderator
Posts: 414
Joined: Fri Sep 07, 2007 3:44 pm
Location: Sandy, UK
Contact:

#9

Postby Steve » Wed Jun 14, 2017 10:18 am

Yes, you are right... a service does not necessarily have to return a response body.

Steve
0 x

Steve James
Ebase User
Posts: 331
Joined: Mon Mar 10, 2014 8:34 am

#10

Postby Steve James » Wed Jun 14, 2017 10:29 am

It would also have helped if the supplier had given me an up to date version of their documentation :roll:
It turns out their API has changed and I wasn't quite doing as the new endpoint expects.

All fun and games and it's helped for a few other things that are going on.

Thanks for your help as ever.
0 x


Who is online

Users browsing this forum: No registered users and 148 guests