mirror of
https://github.com/node-red/node-red.git
synced 2023-10-10 13:36:53 +02:00
add multipart/form-data support to http request node
This commit is contained in:
parent
a6ef755139
commit
a9bf3d0226
@ -129,7 +129,18 @@
|
|||||||
<p>If <code>msg.payload</code> is an Object, the node will automatically set the content type
|
<p>If <code>msg.payload</code> is an Object, the node will automatically set the content type
|
||||||
of the request to <code>application/json</code> and encode the body as such.</p>
|
of the request to <code>application/json</code> and encode the body as such.</p>
|
||||||
<p>To encode the request as form data, <code>msg.headers["content-type"]</code> should be set to <code>application/x-www-form-urlencoded</code>.</p>
|
<p>To encode the request as form data, <code>msg.headers["content-type"]</code> should be set to <code>application/x-www-form-urlencoded</code>.</p>
|
||||||
|
<p>For form-based file uploads, some services require that <code>msg.headers["content-type"]</code> be set to <code>multipart/form-data</code>.
|
||||||
|
To construct <code>msg.payload</code> for this type of request, use the following structure and substitue the <code>KEY</code>, <code>DATA</code>, and <code>FILENAME</code>
|
||||||
|
values for your own.
|
||||||
|
<pre><code>req.formData = {
|
||||||
|
KEY: {
|
||||||
|
value: DATA,
|
||||||
|
options: {
|
||||||
|
filename: ‘FILENAME’
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
</code></pre>
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<script type="text/javascript">
|
<script type="text/javascript">
|
||||||
|
@ -78,6 +78,7 @@ module.exports = function(RED) {
|
|||||||
}
|
}
|
||||||
var opts = {};
|
var opts = {};
|
||||||
opts.url = url;
|
opts.url = url;
|
||||||
|
opts.formData = {};
|
||||||
opts.timeout = node.reqTimeout;
|
opts.timeout = node.reqTimeout;
|
||||||
opts.method = method;
|
opts.method = method;
|
||||||
opts.headers = {};
|
opts.headers = {};
|
||||||
@ -144,6 +145,9 @@ module.exports = function(RED) {
|
|||||||
var payload = null;
|
var payload = null;
|
||||||
|
|
||||||
if (method !== 'GET' && method !== 'HEAD' && typeof msg.payload !== "undefined") {
|
if (method !== 'GET' && method !== 'HEAD' && typeof msg.payload !== "undefined") {
|
||||||
|
if (opts.headers['content-type'] == 'multipart/form-data' && typeof payload === "object") {
|
||||||
|
opts.formData = msg.payload;
|
||||||
|
} else {
|
||||||
if (typeof msg.payload === "string" || Buffer.isBuffer(msg.payload)) {
|
if (typeof msg.payload === "string" || Buffer.isBuffer(msg.payload)) {
|
||||||
payload = msg.payload;
|
payload = msg.payload;
|
||||||
} else if (typeof msg.payload == "number") {
|
} else if (typeof msg.payload == "number") {
|
||||||
@ -167,6 +171,8 @@ module.exports = function(RED) {
|
|||||||
}
|
}
|
||||||
opts.body = payload;
|
opts.body = payload;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
}
|
||||||
// revert to user supplied Capitalisation if needed.
|
// revert to user supplied Capitalisation if needed.
|
||||||
if (opts.headers.hasOwnProperty('content-type') && (ctSet !== 'content-type')) {
|
if (opts.headers.hasOwnProperty('content-type') && (ctSet !== 'content-type')) {
|
||||||
opts.headers[ctSet] = opts.headers['content-type'];
|
opts.headers[ctSet] = opts.headers['content-type'];
|
||||||
|
Loading…
Reference in New Issue
Block a user