File upload (#20)

* Add Support for FileUpload
This commit is contained in:
Frank Jogeleit 2021-01-24 13:14:13 +01:00 committed by GitHub
parent de202bb090
commit b6bb4fa030
WARNING! Although there is a key with this ID in the database it does not verify this commit! This commit is SUSPICIOUS.
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 1715 additions and 7 deletions

View file

@ -42,3 +42,16 @@ jobs:
method: 'GET'
username: 'postman'
password: 'password'
- name: Create Test File
id: image
run: |
echo "test" > testfile.txt
- name: Request Postment Echo POST Multipart
uses: ./
with:
url: 'https://postman-echo.com/post'
method: 'POST'
data: '{ "key": "value" }'
files: '{ "file": "${{ github.workspace }}/testfile.txt" }'

View file

@ -25,6 +25,7 @@ jobs:
|method | Request Method| POST |
|contentType | Request ContentType| application/json |
|data | Request Body Content as JSON String, only for POST / PUT / PATCH Requests | '{}' |
|files | Map of key / absolute file paths send as multipart/form-data request to the API, if set the contentType is set to multipart/form-data, values provided by data will be added as additional FormData values, nested objects are not supported. **Example provided in the _test_ Workflow of this Action** | '{}' |
|timeout| Request Timeout in ms | 5000 (5s) |
|username| Username for Basic Auth ||
|password| Password for Basic Auth ||

View file

@ -16,6 +16,10 @@ inputs:
description: 'Request Body as JSON String'
required: false
default: '{}'
files:
description: 'Map of absolute file paths as JSON String'
required: false
default: '{}'
username:
description: 'Auth Username'
required: false

1586
dist/index.js vendored

File diff suppressed because one or more lines are too long

43
package-lock.json generated
View file

@ -1,6 +1,6 @@
{
"name": "http-request-action",
"version": "1.6.1",
"version": "1.7.0",
"lockfileVersion": 1,
"requires": true,
"dependencies": {
@ -15,6 +15,11 @@
"resolved": "https://registry.npmjs.org/@zeit/ncc/-/ncc-0.22.3.tgz",
"integrity": "sha512-jnCLpLXWuw/PAiJiVbLjA8WBC0IJQbFeUwF4I9M+23MvIxTxk5pD4Q8byQBSPmHQjz5aBoA7AKAElQxMpjrCLQ=="
},
"asynckit": {
"version": "0.4.0",
"resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz",
"integrity": "sha1-x57Zf380y48robyXkLzDZkdLS3k="
},
"axios": {
"version": "0.21.1",
"resolved": "https://registry.npmjs.org/axios/-/axios-0.21.1.tgz",
@ -23,10 +28,46 @@
"follow-redirects": "^1.10.0"
}
},
"combined-stream": {
"version": "1.0.8",
"resolved": "https://registry.npmjs.org/combined-stream/-/combined-stream-1.0.8.tgz",
"integrity": "sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg==",
"requires": {
"delayed-stream": "~1.0.0"
}
},
"delayed-stream": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/delayed-stream/-/delayed-stream-1.0.0.tgz",
"integrity": "sha1-3zrhmayt+31ECqrgsp4icrJOxhk="
},
"follow-redirects": {
"version": "1.13.1",
"resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.13.1.tgz",
"integrity": "sha512-SSG5xmZh1mkPGyKzjZP8zLjltIfpW32Y5QpdNJyjcfGxK3qo3NDDkZOZSFiGn1A6SclQxY9GzEwAHQ3dmYRWpg=="
},
"form-data": {
"version": "3.0.0",
"resolved": "https://registry.npmjs.org/form-data/-/form-data-3.0.0.tgz",
"integrity": "sha512-CKMFDglpbMi6PyN+brwB9Q/GOw0eAnsrEZDgcsH5Krhz5Od/haKHAX0NmQfha2zPPz0JpWzA7GJHGSnvCRLWsg==",
"requires": {
"asynckit": "^0.4.0",
"combined-stream": "^1.0.8",
"mime-types": "^2.1.12"
}
},
"mime-db": {
"version": "1.45.0",
"resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.45.0.tgz",
"integrity": "sha512-CkqLUxUk15hofLoLyljJSrukZi8mAtgd+yE5uO4tqRZsdsAJKv0O+rFMhVDRJgozy+yG6md5KwuXhD4ocIoP+w=="
},
"mime-types": {
"version": "2.1.28",
"resolved": "https://registry.npmjs.org/mime-types/-/mime-types-2.1.28.tgz",
"integrity": "sha512-0TO2yJ5YHYr7M2zzT7gDU1tbwHxEUWBCLt0lscSNpcdAfFyJOVEpRYNS7EXVcTLNj/25QO8gulHC5JtTzSE2UQ==",
"requires": {
"mime-db": "1.45.0"
}
}
}
}

View file

@ -1,6 +1,6 @@
{
"name": "http-request-action",
"version": "1.6.1",
"version": "1.7.0",
"description": "",
"main": "src/index.js",
"private": false,
@ -23,6 +23,7 @@
},
"dependencies": {
"@zeit/ncc": "^0.22",
"axios": "^0.21.1"
"axios": "^0.21.1",
"form-data": "^3.0.0"
}
}

View file

@ -1,9 +1,11 @@
const axios = require("axios");
const FormData = require('form-data')
const fs = require('fs')
const METHOD_GET = 'GET'
const METHOD_POST = 'POST'
const request = async({ method, instanceConfig, data, auth, actions, preventFailureOnNoResponse, escapeData }) => {
const request = async({ method, instanceConfig, data, files, auth, actions, preventFailureOnNoResponse, escapeData }) => {
try {
const instance = axios.create(instanceConfig);
@ -17,6 +19,21 @@ const request = async({ method, instanceConfig, data, auth, actions, preventFail
data = undefined;
}
if (files && files !== '{}') {
filesJson = convertToJSON(files)
dataJson = convertToJSON(data)
if (Object.keys(filesJson).length > 0) {
try {
data = convertToFormData(dataJson, filesJson)
instanceConfig = await updateConfig(instanceConfig, data, actions)
} catch(error) {
actions.setFailed({ message: `Unable to convert Data and Files into FormData: ${error.message}`, data: dataJson, files: filesJson })
return
}
}
}
const requestData = {
auth,
method,
@ -45,6 +62,54 @@ const request = async({ method, instanceConfig, data, auth, actions, preventFail
}
}
const convertToJSON = (value) => {
try {
return JSON.parse(value)
} catch(e) {
return {}
}
}
const convertToFormData = (data, files) => {
formData = new FormData()
for (const [key, value] of Object.entries(data)) {
formData.append(key, value)
}
for (const [key, value] of Object.entries(files)) {
formData.append(key, fs.createReadStream(value))
}
return formData
}
const updateConfig = async (instanceConfig, formData, actions) => {
try {
return {
...instanceConfig,
headers: {
...instanceConfig.headers,
...formData.getHeaders(),
'Content-Length': await contentLength(formData)
}
}
} catch(error) {
actions.setFailed({ message: `Unable to read Content-Length: ${error.message}`, data, files })
}
}
const contentLength = (formData) => new Promise((resolve, reject) => {
formData.getLength((err, length) => {
if (err) {
reject (err)
return
}
resolve(length)
})
})
module.exports = {
request,
METHOD_POST,

View file

@ -37,8 +37,9 @@ const instanceConfig = {
core.debug('Instance Configuration: ' + JSON.stringify(instanceConfig))
const data = core.getInput('data') || '{}';
const files = core.getInput('files') || '{}';
const method = core.getInput('method') || METHOD_POST;
const preventFailureOnNoResponse = core.getInput('preventFailureOnNoResponse') === 'true';
const escapeData = core.getInput('escapeData') === 'true';
request({ data, method, instanceConfig, auth, preventFailureOnNoResponse, escapeData, actions: new GithubActions() })
request({ data, method, instanceConfig, auth, preventFailureOnNoResponse, escapeData, files, actions: new GithubActions() })