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
REST - get set request body
Moderators: Jon, Steve, Ian, Dave
-
- Moderator
- Posts: 419
- Joined: Fri Sep 07, 2007 3:44 pm
- Location: Sandy, UK
- Contact:
Hi Steve,
You need to do something like this:
I hope this helps...
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();
}
Steve
0 x
-
- Ebase User
- Posts: 331
- Joined: Mon Mar 10, 2014 8:34 am
Thanks Steve, I'm getting an error
ie the string I need to pass into the body.
Thanks
Code: Select all
TypeError: Cannot find function getBytes in object {"serviceIds":["4c2fe2853f1f4b429b928faabc8b35e6c6a3154ff883163f4149676e4a84df00","df20bd853e13c5b71715f3f332ec304839637ac8b34cdd3c20c9820f01377dd9"]}. (run#43)
Thanks
0 x
-
- Moderator
- Posts: 419
- Joined: Fri Sep 07, 2007 3:44 pm
- Location: Sandy, UK
- Contact:
Ah yes, you'll need to create a Java String:Steve James wrote:Thanks Steve, I'm getting an error
ie the string I need to pass into the body.Code: Select all
TypeError: Cannot find function getBytes in object {"serviceIds":["4c2fe2853f1f4b429b928faabc8b35e6c6a3154ff883163f4149676e4a84df00","df20bd853e13c5b71715f3f332ec304839637ac8b34cdd3c20c9820f01377dd9"]}. (run#43)
Thanks
Change :
Code: Select all
var input = "{"qty":100,"name":"iPad 4"}";
Code: Select all
var input = new java.lang.String("{"qty":100,"name":"iPad 4"}");
0 x
-
- Ebase User
- Posts: 331
- Joined: Mon Mar 10, 2014 8:34 am
-
- Ebase User
- Posts: 331
- Joined: Mon Mar 10, 2014 8:34 am
-
- Moderator
- Posts: 419
- Joined: Fri Sep 07, 2007 3:44 pm
- Location: Sandy, UK
- Contact:
Hi Steve,
You should be able to do something like:
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);
0 x
-
- Moderator
- Posts: 419
- Joined: Fri Sep 07, 2007 3:44 pm
- Location: Sandy, UK
- Contact:
-
- Moderator
- Posts: 419
- Joined: Fri Sep 07, 2007 3:44 pm
- Location: Sandy, UK
- Contact:
-
- Ebase User
- Posts: 331
- Joined: Mon Mar 10, 2014 8:34 am
Who is online
Users browsing this forum: No registered users and 6 guests