Help using java to access API

Discussions about Z-Way software and Z-Wave technology in general
Post Reply
snesbittsea
Posts: 3
Joined: 29 Feb 2016 15:48

Help using java to access API

Post by snesbittsea »

I'm looking for a java code example that demonstrates logging in and then accessing the api.

Currently I'm sending an HTTP post along with an HttpContext, but I'm not getting back the expected cookie.

Thanks in advance,

-steve
User avatar
DomotikQuebec
Posts: 34
Joined: 14 Jan 2016 19:06
Location: Quebec City, Canada
Contact:

Re: Help using java to access API

Post by DomotikQuebec »

To help you, some code you are using may guide us to what you lack.
Mario Gravel
Domotik Quebec
snesbittsea
Posts: 3
Joined: 29 Feb 2016 15:48

Re: Help using java to access API

Post by snesbittsea »

I finally got it to work by adding a string representation of a json object to the entity.

Here's my initial code. Based on the API documentation section 3.4.1 I thought this should work. In this case, however, no cookie is returned.(Note that this is written in groovy using the Apache HttpClient library).

Code: Select all

def uri = new URIBuilder()
        .setScheme("http")
        .setHost('xxx.xxx.xxx.xxx:8083')
        .setPath('/ZAutomation/api/v1/login')
        .build()

HttpClient client = HttpClientBuilder.create().build();
CookieStore cookies = new BasicCookieStore();
HttpPost request = new HttpPost(uri);
HttpContext httpContext = new BasicHttpContext();
httpContext.setAttribute(HttpClientContext.COOKIE_STORE, cookies);

List <NameValuePair> paramList = new ArrayList <NameValuePair>();
paramList.add(new BasicNameValuePair("login", 'xxxx'));
paramList.add(new BasicNameValuePair("password", 'xxxx'));
request.setEntity(new UrlEncodedFormEntity(paramList));

response = client.execute(request, httpContext);
Here's the code that finally worked, The difference is that rather than passing in login parameters as key/value pairs, I pass them in in the entity object as a stringified version of a json object.

Code: Select all

uri = new URIBuilder()
    .setScheme("http")
    .setHost('xxx.xxx.xxx.xxx:8083')
    .setPath('/ZAutomation/api/v1/login')
    .build()

HttpClient client = HttpClientBuilder.create().build();
CookieStore cookies = new BasicCookieStore();
HttpContext httpContext = new BasicHttpContext();
httpContext.setAttribute(HttpClientContext.COOKIE_STORE, cookies);
String json = '{"form":"true", "login":"xxx","password":"xxx"}';

StringEntity entity = new StringEntity(json)
HttpPost loginRequest = new HttpPost(uri);
loginRequest.setEntity(entity)
loginRequest.setHeader("Accept", "application/json");
loginRequest.setHeader("Content-type", "application/json");

response = client.execute(loginRequest, httpContext);
User avatar
DomotikQuebec
Posts: 34
Joined: 14 Jan 2016 19:06
Location: Quebec City, Canada
Contact:

Re: Help using java to access API

Post by DomotikQuebec »

The problem seems to be the URLEncoding... It is use when you do a GET Request and you want your JSON in the "URL"
When posting, don't ! ;-) It will screw your json.
for example, a space will become %20
Mario Gravel
Domotik Quebec
Post Reply