Page 1 of 2

HTTP Request

Posted: 22 Apr 2014 21:10
by enco.josh
I am running the newest 1.5.0 release. I see that there is a http object. Is there any documentation on the http.request function? Does it support https?

Re: HTTP Request

Posted: 22 Apr 2014 22:03
by boomhauser
I found what I needed in the "Z-Way Developers Documentation". There is a section on JSON API that somewhat describes the HTTP interface. You could start there. It is also clear by inspecting the GUI code what some of the capabilities are.

I have no idea if that HTTP server supports HTTPS -- I set up a proxy in my environment.

Re: HTTP Request

Posted: 24 Apr 2014 02:36
by pofs
The http.request is much like jQuery.ajax():

Code: Select all

r = http.request(options);
Here's the list of options:
  • url – required. Url you want to request (might be http, https, or maybe even ftp);
  • method – optional. Http method to use (currently one of GET, POST, HEAD). If not specified, GET is used;
  • headers – optional. Object containing additional headers to pass to server:

    Code: Select all

    headers: {
        "Content-Type": "text/xml",
        "X-Requested-With": "RaZberry/1.5.0"
    }
  • data – required for POST requests. Data to post to the server. May be either string (to post raw data) or an object with keys and values (will be serialized as "key1=value1&key2=value2&…");
  • auth – optional. Provides credentials for basic authentication. It is an object containing login and password:

    Code: Select all

    auth: {
        login: "username",
        password: "secret"
    }
  • contentType – optional. Allows to override content type returned by server for parsing data (see below);
  • async – optional. Specifies whether request should be sent asynchronously. Default is false. In case of synchronous request result is returned immediately (as function return value), otherwise function exits immediately, and response is delivered later thru callbacks.
  • success, error and complete – optional, valid only for async requests. Success callback is called after successful request, error is called on failure, complete is called nevertheless (even if success/error callback produces exception, so it is like "finally" statement);
Response (as stated above) is delivered either as function return value, or as callback parameter. Is is always an object containing following members:
  • status – HTTP status code (or -1 if some non-HTTP error occurred). Status codes from 200 to 299 are considered success;
  • statusText – status string;
  • url – response url (might differ from url requested in case of server redirects);
  • headers – object containing all the headers returned by server;
  • contentType – content type returned by server;
  • data – response data.
Response data is handled differently depending on content type (if contentType on request is set, it takes priority over server content type):
  • application/json and text/x-json are returned as JSON object;
  • application/xml and text/xml are returned as XML object;
  • application/octet-stream is returned as binary ArrayBuffer;
  • string is returned otherwise.
In case data cannot be parsed as valid JSON/XML, it is still returned as string, and additional parseError member is present.

Re: HTTP Request

Posted: 13 Jun 2014 19:21
by enco.josh
What are the arguments for the success, error, and complete callbacks?

Re: HTTP Request

Posted: 14 Jun 2014 03:57
by PoltoS
All three have same parameter described above:
an object with status, statusText, url, headers, contentType and data properties.

Re: HTTP Request

Posted: 07 Jul 2014 23:08
by raZcherry
Hi All,
I want to create a HTTP request object in a module with the following code (using Razberry v.1.7.1)

Code: Select all

http.request({
          url: "IP TO MY SERVER",
          method: "POST",
          headers: {
            "Content-Type" : "application/json"},
          data: "",
        });
My server checks if the content-type is set to "application/json" otherwise he will throw an error.

I sniffed the packets with Wireshark from ZWAY --> My Server and the result is:
http post request.JPG
http post request.JPG (13.58 KiB) Viewed 15158 times
http post request 2.JPG
http post request 2.JPG (18.51 KiB) Viewed 15158 times
It seems to be that on a post request the headers with "Content-Type" : "application/json" will not correctly encoded. The same for "text/xml" . %2F in picture two is the URL-encoding value for /.
If it is correctly encoded, it would look like
http post request 3.JPG
http post request 3.JPG (13.18 KiB) Viewed 15158 times
Can somebody reproduce the issue and maybe give a hint how to fix this?
I am also interest in using XMLHttpRequest Object(like request = new XMLHttpRequest(); ) and maybe modify the http.request() method (unfortunately I can´t find the method in .js files)
Thanks in advance for your responses! ;)

Re: HTTP Request

Posted: 08 Jul 2014 10:19
by pz1
I am also interest in using XMLHttpRequest Object(like request = new XMLHttpRequest(); )
RaZberry has it's own implementation. See in this post:
viewtopic.php?f=3422&t=19972&hilit=+xml#p50960

Due to other things not working I haven't had the time to look into this.

Re: HTTP Request

Posted: 08 Jul 2014 11:29
by PoltoS
We have an issue with URL-encodede Content-Type. This will be fixed soon,

Re: HTTP Request

Posted: 09 Jul 2014 15:15
by raZcherry
Thanks for your answers, pz1 and PoltoS.
Would be kind to post here, if there are any news of the issue :)

I want to ask if it is possible to see the source code of http.request() function in z-way? I found only on automation/htdocs/js/helpers/apis.js the function request() and it is using jQuery (and I just thought jQuery is not available, when programming in backend - but maybe I am wrong and I can load it somehow ;) )

I want to use XMLHttpRequest for all the communication (not only for sending and receiving xml) from ZWay --> MyServer instead of using http.request, like

Code: Select all

var xmlhttp = new XMLHttpRequest();   // new HttpRequest instance 
xmlhttp.open("POST", "/json-handler");
xmlhttp.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
Maybe there is a way how to use it in ZWay?

Thanks in advance for your responses!

Re: HTTP Request

Posted: 13 Jul 2014 20:31
by PoltoS
don't mix client and server side.

automation/htdocs/* is client side UI for automation. And indeed it uses jQuery.

On server side it is not available. http.request code is our function, XMLHttpRequest is not available, as it is an extension of browsers.