aboutsummaryrefslogtreecommitdiffstats
path: root/dgbuilder/core_nodes/io/21-httpin.html
blob: 059b8596e64296c628f7bd6caee11f1c888c2f3a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
<!--
  Copyright 2013 IBM Corp.

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->

<script type="text/x-red" data-template-name="http in">
    <div class="form-row">
        <label for="node-input-method"><i class="fa fa-tasks"></i> Method</label>
        <select type="text" id="node-input-method" style="width:72%;">
        <option value="get">GET</option>
        <option value="post">POST</option>
        <option value="put">PUT</option>
        <option value="delete">DELETE</option>
        </select>
    </div>
    <div class="form-row">
        <label for="node-input-url"><i class="fa fa-globe"></i> url</label>
        <input type="text" id="node-input-url" placeholder="/url">
    </div>
    <div class="form-row">
        <label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
        <input type="text" id="node-input-name" placeholder="Name">
    </div>
    <div id="node-input-tip" class="form-tips">The url will be relative to <code><span id="node-input-path"></span></code>.</div>
</script>

<script type="text/x-red" data-help-name="http in">
    <p>Provides an input node for http requests, allowing the creation of simple web services.</p>
    <p>The resulting message has the following properties:
        <ul>
            <li>msg.req : <a href="http://expressjs.com/api.html#req">http request</a></li>
            <li>msg.res : <a href="http://expressjs.com/api.html#res">http response</a></li>
        </ul>
    </p>
    <p>For POST/PUT requests, the body is available under <code>msg.req.body</code>. This
       uses the <a href="http://expressjs.com/api.html#bodyParser">Express bodyParser middleware</a> to parse the content to a JSON object.
    </p>
    <p>
       By default, this expects the body of the request to be url encoded:
       <pre>foo=bar&amp;this=that</pre>
    </p>
    <p>
       To send JSON encoded data to the node, the content-type header of the request must be set to
       <code>application/json</code>.
    </p>
    <p>
       <b>Note: </b>This node does not send any response to the http request. This should be done with
       a subsequent HTTP Response node, or Function node.
       In the case of a Function node, the <a href="http://expressjs.com/api.html#res">Express response documentation</a>
       describes how this should be done. For example:
       <pre>msg.res.send(200, 'Thanks for the request ');<br/>return msg;</pre>
    </p>

</script>

<script type="text/x-red" data-template-name="http response">
    <div class="form-row">
        <label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
        <input type="text" id="node-input-name" placeholder="Name">
    </div>
    <div class="form-tips">The messages sent to this node <b>must</b> originate from an <i>http input</i> node</div>
</script>

<script type="text/x-red" data-help-name="http response">
    <p>Sends responses back to http requests received from an HTTP Input node.</p>
    <p>The response can be customised using the following message properties:</p>
    <ul>
        <li><code>payload</code> is sent as the body of the response</li>
        <li><code>statusCode</code>, if set, is used as the response status code (default: 200)</li>
        <li><code>headers</code>, if set, should be an object containing field/value
        pairs to be added as response headers.</li>
    </ul>
</script>

<script type="text/x-red" data-template-name="http request">
    <div class="form-row">
        <label for="node-input-method"><i class="fa fa-tasks"></i> Method</label>
        <select type="text" id="node-input-method" style="width:72%;">
        <option value="GET">GET</option>
        <option value="POST">POST</option>
        <option value="PUT">PUT</option>
        <option value="DELETE">DELETE</option>
        </select>
    </div>
    <div class="form-row">
        <label for="node-input-url"><i class="fa fa-globe"></i> URL</label>
        <input type="text" id="node-input-url" placeholder="http://">
    </div>
    <div class="form-row">
        <label>&nbsp;</label>
        <input type="checkbox" id="node-input-useAuth" style="display: inline-block; width: auto; vertical-align: top;">
        <label for="node-input-useAuth" style="width: 70%;">Use basic authentication?</label>
    </div>
    <div class="form-row node-input-useAuth-row">
        <label for="node-input-user"><i class="fa fa-user"></i> Username</label>
        <input type="text" id="node-input-user">
    </div>
    <div class="form-row node-input-useAuth-row">
        <label for="node-input-password"><i class="fa fa-lock"></i> Password</label>
        <input type="password" id="node-input-password">
    </div>
    <div class="form-row">
        <label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
        <input type="text" id="node-input-name" placeholder="Name">
    </div>
</script>

<script type="text/x-red" data-help-name="http request">
    <p>Provides a node for making http requests.</p>
    <p>The URL and HTTP method can be configured in the node, but also
       overridden by the incoming message:
    <ul>
        <li><code>url</code>, if set, is used as the url of the request. Must start with http: or https:</li>
        <li><code>method</code>, if set, is used as the HTTP method of the request.
        Must be one of <code>GET</code>, <code>PUT</code>, <code>POST</code> or <code>DELETE</code> (default: GET)</li>
        <li><code>headers</code>, if set, should be an object containing field/value
        pairs to be added as request headers</li>
        <li><code>payload</code> is sent as the body of the request</li>
    </ul>
    <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
    url to be constructed using values of the incoming message. For example, if the url is set to
    <code>example.com/{{topic}}</code>, it will have the value of <code>msg.topic</code> automatically inserted.</p>
    <p>
    The output message contains the following properties:
    <ul>
        <li><code>payload</code> is the body of the response</li>
        <li><code>statusCode</code> is the status code of the response, or the error code if the request could not be completed</li>
        <li><code>headers</code> is an object containing the response headers</li>
    </ul>
</script>

<script type="text/javascript">
    RED.nodes.registerType('http in',{
        category: 'input',
        color:"rgb(231, 231, 174)",
        defaults: {
            name: {value:""},
            url: {value:"",required:true},
            method: {value:"get",required:true}
        },
        inputs:0,
        outputs:1,
        icon: "white-globe.png",
        label: function() {
            if (this.name) {
                return this.name;
            } else if (this.url) {
                var root = RED.settings.httpNodeRoot;
                if (root.slice(-1) != "/") { 
                    root = root+"/";
                }
                if (this.url.charAt(0) == "/") {
                    root += this.url.slice(1);
                } else {
                    root += this.url;
                }
                return "["+this.method+"] "+root;
            } else {
                return "http";
            }
        },
        labelStyle: function() {
            return this.name?"node_label_italic":"";
        },
        oneditprepare: function() {
            var root = RED.settings.httpNodeRoot;
            if (root.slice(-1) == "/") { 
                root = root.slice(0,-1);
            }
            if (root == "") {
                $("#node-input-tip").hide();
            } else {
                $("#node-input-path").html(root);
                $("#node-input-tip").show();
            }
            //document.getElementById("node-config-wsdocpath").innerHTML=
        }

    });

    RED.nodes.registerType('http response',{
        category: 'output',
        color:"rgb(231, 231, 174)",
        defaults: {
            name: {value:""}
        },
        inputs:1,
        outputs:0,
        align: "right",
        icon: "white-globe.png",
        label: function() {
            return this.name||"http";
        },
        labelStyle: function() {
            return this.name?"node_label_italic":"";
        }
    });

    RED.nodes.registerType('http request',{
        category: 'function',
        color:"rgb(231, 231, 174)",
        defaults: {
            name: {value:""},
            method:{value:"GET"},
            url:{value:""},
            //user -> credentials
            //pass -> credentials
        },
        credentials: {
            user: {type:"text"},
            password: {type: "password"}
        },
        inputs:1,
        outputs:1,
        align: "right",
        icon: "white-globe.png",
        label: function() {
            return this.name||"http request";
        },
        labelStyle: function() {
            return this.name?"node_label_italic":"";
        },
        oneditprepare: function() {
            if (this.credentials.user || this.credentials.has_password) {
                $('#node-input-useAuth').prop('checked', true);
                $(".node-input-useAuth-row").show();
            } else {
                $('#node-input-useAuth').prop('checked', false);
                $(".node-input-useAuth-row").hide();
            }

            $("#node-input-useAuth").change(function() {
                if ($(this).is(":checked")) {
                    $(".node-input-useAuth-row").show();
                } else {
                    $(".node-input-useAuth-row").hide();
                    $('#node-input-user').val('');
                    $('#node-input-password').val('');
                }
            });
        },
    });
</script>