0

I'm trying to read a JSON response from a RESTful webserver running on an IoT module (Advantech WISE-4012). According to the documentation, any GET request should be made in this form

GET /ai_value/slot_0/ch_0

Any Java implementation of GET requests (Java libraries, Apache etc.), anyway, append to the end of the request the protocol signature HTTP/1.1. E.g:

GET http://192.168.0.14/ai_value/slot_0/ch_0 HTTP/1.1

Because of this (probably) i'm getting Error 400 (Bad request) on every client i tried so far. The only working method i've discovered was sending a simple request through the address bar on Google Chrome browser (sometimes i get a response, sometimes a get a bad request error either). How can i write a java implementation of a GET request plain and simple as described by the documentation? How can i test a custom GET request without HTTP/1.1 at the end? Every chrome extension i tried (Advanced REST Client, Postman) add the protocol version at the end, so i haven't had the chance to verify if that's why i'm getting a bad request error.

EDIT:

This is the response header from Advanced REST client

Connection: close
Content-Type: application/json
Server: WISE-4000/8.1.0020

While the source message is the following one:

GET /ai_value/slot_0/ch_0 HTTP/1.1
HOST: 192.168.0.14

The only mismatch between the documentation is the HTTP/1.1 signature as mentioned before. Adding the "accept: application/json" makes no difference either

Mastarius
  • 193
  • 2
  • 11
  • Check the response headers and/or body and see if there's any error message? – Darshan Mehta Jun 15 '17 at 08:07
  • @DarshanMehta i added the response and the source message on my post. Thank you – Mastarius Jun 15 '17 at 08:26
  • Can you try using `curl` as well, from command line? – Darshan Mehta Jun 15 '17 at 11:07
  • @DarshanMehta i'm on windows environment but i'll figure out how to do something like that. Any suggestion? – Mastarius Jun 15 '17 at 12:06
  • https://stackoverflow.com/questions/9507353/how-do-i-install-set-up-and-use-curl-on-windows – Darshan Mehta Jun 15 '17 at 12:48
  • @DarshanMehta i tried using curl as mentioned. `curl -i -H "Accept: application/json" -H "Content-Type: application/json" -X GET http://192.168.0.14/ai_value/slot_0/ch_0` gives me always the same response: `HTTP/1.0 Bad request 400` and same response headers as the other tests. I'm wondering why sometimes it does work by calling it on chrome, otherwise i'd assume that my device is not working. – Mastarius Jun 15 '17 at 13:03
  • 1
    Could you point me to the documentation of this API? – Darshan Mehta Jun 15 '17 at 13:16
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/146768/discussion-between-mastarius-and-darshan-mehta). – Mastarius Jun 15 '17 at 13:18

2 Answers2

1

After a bit of digging into the documentation, it looks like the default timeout (i.e. 720 seconds) is the one causing an issue. There doesn't seem to be any way to work it around (ideally, the system should reset the time after a successful request and we should only get 400 - or 403 ideally after 720 seconds of inactivity).

A couple of points I would like to recommend to the API developers for WISE-4012 (if they are in touch with you):

  • Add brief documentation for authentication and timeout (probably, more response codes and error messages with each error response)
  • Enable OAuth for API Access

As far as current implentation is conerned, I guess you need to do a basic auth and pass username/password with every request, Or add Authentication header with every API request to get successful response without any 400s.

Darshan Mehta
  • 27,835
  • 7
  • 53
  • 81
  • Thank you again, i'd like to add that setting another header called Authentication with the correct value (as checked using Postman) solved the problem also on my java software. – Mastarius Jun 15 '17 at 15:08
  • could you elaborate more on that ? How did you construct the authentication token for the default user and password ? Thanks! – javanoob May 03 '21 at 21:37
0

Check if this helps.

HttpClient httpClient = HttpClients.createDefault();
URI reqUri = new URI(<uri>);
RequestBuilder requestBuilder = RequestBuilder.create("GET");
requestBuilder.setUri(reqUri);
requestBuilder.setHeader(<headerKey>, <headerValue>);
requestBuilder.setEntity(<entity_data>);
HttpUriRequest httpRequest = requestBuilder.build();
httpResponse = httpClient.execute(httpRequest);
paulophoenix
  • 69
  • 1
  • 9
  • Unfortunately i get this kind of response when i try to print the result of this HTTP Request. `HttpResponseProxy{HTTP/1.0 400 Bad Request [Server: WISE-4000/8.1.0020, Connection: close, Content-type: application/json] ResponseEntityProxy{[Content-Type: application/json,Chunked: false]}}` – Mastarius Jun 15 '17 at 09:03