[CCSDK-28] populated the seed code for dgbuilder
[ccsdk/distribution.git] / dgbuilder / core_nodes / io / 21-httpin.html
1 <!--
2   Copyright 2013 IBM Corp.
3
4   Licensed under the Apache License, Version 2.0 (the "License");
5   you may not use this file except in compliance with the License.
6   You may obtain a copy of the License at
7
8   http://www.apache.org/licenses/LICENSE-2.0
9
10   Unless required by applicable law or agreed to in writing, software
11   distributed under the License is distributed on an "AS IS" BASIS,
12   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   See the License for the specific language governing permissions and
14   limitations under the License.
15 -->
16
17 <script type="text/x-red" data-template-name="http in">
18     <div class="form-row">
19         <label for="node-input-method"><i class="fa fa-tasks"></i> Method</label>
20         <select type="text" id="node-input-method" style="width:72%;">
21         <option value="get">GET</option>
22         <option value="post">POST</option>
23         <option value="put">PUT</option>
24         <option value="delete">DELETE</option>
25         </select>
26     </div>
27     <div class="form-row">
28         <label for="node-input-url"><i class="fa fa-globe"></i> url</label>
29         <input type="text" id="node-input-url" placeholder="/url">
30     </div>
31     <div class="form-row">
32         <label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
33         <input type="text" id="node-input-name" placeholder="Name">
34     </div>
35     <div id="node-input-tip" class="form-tips">The url will be relative to <code><span id="node-input-path"></span></code>.</div>
36 </script>
37
38 <script type="text/x-red" data-help-name="http in">
39     <p>Provides an input node for http requests, allowing the creation of simple web services.</p>
40     <p>The resulting message has the following properties:
41         <ul>
42             <li>msg.req : <a href="http://expressjs.com/api.html#req">http request</a></li>
43             <li>msg.res : <a href="http://expressjs.com/api.html#res">http response</a></li>
44         </ul>
45     </p>
46     <p>For POST/PUT requests, the body is available under <code>msg.req.body</code>. This
47        uses the <a href="http://expressjs.com/api.html#bodyParser">Express bodyParser middleware</a> to parse the content to a JSON object.
48     </p>
49     <p>
50        By default, this expects the body of the request to be url encoded:
51        <pre>foo=bar&amp;this=that</pre>
52     </p>
53     <p>
54        To send JSON encoded data to the node, the content-type header of the request must be set to
55        <code>application/json</code>.
56     </p>
57     <p>
58        <b>Note: </b>This node does not send any response to the http request. This should be done with
59        a subsequent HTTP Response node, or Function node.
60        In the case of a Function node, the <a href="http://expressjs.com/api.html#res">Express response documentation</a>
61        describes how this should be done. For example:
62        <pre>msg.res.send(200, 'Thanks for the request ');<br/>return msg;</pre>
63     </p>
64
65 </script>
66
67 <script type="text/x-red" data-template-name="http response">
68     <div class="form-row">
69         <label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
70         <input type="text" id="node-input-name" placeholder="Name">
71     </div>
72     <div class="form-tips">The messages sent to this node <b>must</b> originate from an <i>http input</i> node</div>
73 </script>
74
75 <script type="text/x-red" data-help-name="http response">
76     <p>Sends responses back to http requests received from an HTTP Input node.</p>
77     <p>The response can be customised using the following message properties:</p>
78     <ul>
79         <li><code>payload</code> is sent as the body of the response</li>
80         <li><code>statusCode</code>, if set, is used as the response status code (default: 200)</li>
81         <li><code>headers</code>, if set, should be an object containing field/value
82         pairs to be added as response headers.</li>
83     </ul>
84 </script>
85
86 <script type="text/x-red" data-template-name="http request">
87     <div class="form-row">
88         <label for="node-input-method"><i class="fa fa-tasks"></i> Method</label>
89         <select type="text" id="node-input-method" style="width:72%;">
90         <option value="GET">GET</option>
91         <option value="POST">POST</option>
92         <option value="PUT">PUT</option>
93         <option value="DELETE">DELETE</option>
94         </select>
95     </div>
96     <div class="form-row">
97         <label for="node-input-url"><i class="fa fa-globe"></i> URL</label>
98         <input type="text" id="node-input-url" placeholder="http://">
99     </div>
100     <div class="form-row">
101         <label>&nbsp;</label>
102         <input type="checkbox" id="node-input-useAuth" style="display: inline-block; width: auto; vertical-align: top;">
103         <label for="node-input-useAuth" style="width: 70%;">Use basic authentication?</label>
104     </div>
105     <div class="form-row node-input-useAuth-row">
106         <label for="node-input-user"><i class="fa fa-user"></i> Username</label>
107         <input type="text" id="node-input-user">
108     </div>
109     <div class="form-row node-input-useAuth-row">
110         <label for="node-input-password"><i class="fa fa-lock"></i> Password</label>
111         <input type="password" id="node-input-password">
112     </div>
113     <div class="form-row">
114         <label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
115         <input type="text" id="node-input-name" placeholder="Name">
116     </div>
117 </script>
118
119 <script type="text/x-red" data-help-name="http request">
120     <p>Provides a node for making http requests.</p>
121     <p>The URL and HTTP method can be configured in the node, but also
122        overridden by the incoming message:
123     <ul>
124         <li><code>url</code>, if set, is used as the url of the request. Must start with http: or https:</li>
125         <li><code>method</code>, if set, is used as the HTTP method of the request.
126         Must be one of <code>GET</code>, <code>PUT</code>, <code>POST</code> or <code>DELETE</code> (default: GET)</li>
127         <li><code>headers</code>, if set, should be an object containing field/value
128         pairs to be added as request headers</li>
129         <li><code>payload</code> is sent as the body of the request</li>
130     </ul>
131     <p>When configured within the node, the URL property can contain <a href="http://mustache.github.io/mustache.5.html" target="_new">mustache-style</a> tags. These allow the
132     url to be constructed using values of the incoming message. For example, if the url is set to
133     <code>example.com/{{topic}}</code>, it will have the value of <code>msg.topic</code> automatically inserted.</p>
134     <p>
135     The output message contains the following properties:
136     <ul>
137         <li><code>payload</code> is the body of the response</li>
138         <li><code>statusCode</code> is the status code of the response, or the error code if the request could not be completed</li>
139         <li><code>headers</code> is an object containing the response headers</li>
140     </ul>
141 </script>
142
143 <script type="text/javascript">
144     RED.nodes.registerType('http in',{
145         category: 'input',
146         color:"rgb(231, 231, 174)",
147         defaults: {
148             name: {value:""},
149             url: {value:"",required:true},
150             method: {value:"get",required:true}
151         },
152         inputs:0,
153         outputs:1,
154         icon: "white-globe.png",
155         label: function() {
156             if (this.name) {
157                 return this.name;
158             } else if (this.url) {
159                 var root = RED.settings.httpNodeRoot;
160                 if (root.slice(-1) != "/") { 
161                     root = root+"/";
162                 }
163                 if (this.url.charAt(0) == "/") {
164                     root += this.url.slice(1);
165                 } else {
166                     root += this.url;
167                 }
168                 return "["+this.method+"] "+root;
169             } else {
170                 return "http";
171             }
172         },
173         labelStyle: function() {
174             return this.name?"node_label_italic":"";
175         },
176         oneditprepare: function() {
177             var root = RED.settings.httpNodeRoot;
178             if (root.slice(-1) == "/") { 
179                 root = root.slice(0,-1);
180             }
181             if (root == "") {
182                 $("#node-input-tip").hide();
183             } else {
184                 $("#node-input-path").html(root);
185                 $("#node-input-tip").show();
186             }
187             //document.getElementById("node-config-wsdocpath").innerHTML=
188         }
189
190     });
191
192     RED.nodes.registerType('http response',{
193         category: 'output',
194         color:"rgb(231, 231, 174)",
195         defaults: {
196             name: {value:""}
197         },
198         inputs:1,
199         outputs:0,
200         align: "right",
201         icon: "white-globe.png",
202         label: function() {
203             return this.name||"http";
204         },
205         labelStyle: function() {
206             return this.name?"node_label_italic":"";
207         }
208     });
209
210     RED.nodes.registerType('http request',{
211         category: 'function',
212         color:"rgb(231, 231, 174)",
213         defaults: {
214             name: {value:""},
215             method:{value:"GET"},
216             url:{value:""},
217             //user -> credentials
218             //pass -> credentials
219         },
220         credentials: {
221             user: {type:"text"},
222             password: {type: "password"}
223         },
224         inputs:1,
225         outputs:1,
226         align: "right",
227         icon: "white-globe.png",
228         label: function() {
229             return this.name||"http request";
230         },
231         labelStyle: function() {
232             return this.name?"node_label_italic":"";
233         },
234         oneditprepare: function() {
235             if (this.credentials.user || this.credentials.has_password) {
236                 $('#node-input-useAuth').prop('checked', true);
237                 $(".node-input-useAuth-row").show();
238             } else {
239                 $('#node-input-useAuth').prop('checked', false);
240                 $(".node-input-useAuth-row").hide();
241             }
242
243             $("#node-input-useAuth").change(function() {
244                 if ($(this).is(":checked")) {
245                     $(".node-input-useAuth-row").show();
246                 } else {
247                     $(".node-input-useAuth-row").hide();
248                     $('#node-input-user').val('');
249                     $('#node-input-password').val('');
250                 }
251             });
252         },
253     });
254 </script>