HTTP Request

Discussions about RaZberry - Z-Wave board for Raspberry computer
enco.josh
Posts: 35
Joined: 10 Mar 2014 22:10

HTTP Request

Post 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?
boomhauser
Posts: 3
Joined: 18 Nov 2013 19:10

Re: HTTP Request

Post 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.
pofs
Posts: 688
Joined: 25 Mar 2011 19:03

Re: HTTP Request

Post 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.
enco.josh
Posts: 35
Joined: 10 Mar 2014 22:10

Re: HTTP Request

Post by enco.josh »

What are the arguments for the success, error, and complete callbacks?
User avatar
PoltoS
Posts: 7571
Joined: 26 Jan 2011 19:36

Re: HTTP Request

Post by PoltoS »

All three have same parameter described above:
an object with status, statusText, url, headers, contentType and data properties.
raZcherry
Posts: 11
Joined: 24 Jun 2014 01:14

Re: HTTP Request

Post 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 12635 times
http post request 2.JPG
http post request 2.JPG (18.51 KiB) Viewed 12635 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 12635 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! ;)
Using: Z-Way v2.0.1-rc11 on Raspberry Pi
pz1
Posts: 2053
Joined: 08 Apr 2012 13:44

Re: HTTP Request

Post 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.
Since 29-12-2016 I am no longer a moderator for this forum
User avatar
PoltoS
Posts: 7571
Joined: 26 Jan 2011 19:36

Re: HTTP Request

Post by PoltoS »

We have an issue with URL-encodede Content-Type. This will be fixed soon,
raZcherry
Posts: 11
Joined: 24 Jun 2014 01:14

Re: HTTP Request

Post 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!
Using: Z-Way v2.0.1-rc11 on Raspberry Pi
User avatar
PoltoS
Posts: 7571
Joined: 26 Jan 2011 19:36

Re: HTTP Request

Post 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.
Post Reply