mirror of
https://github.com/fjogeleit/http-request-action.git
synced 2026-02-04 16:45:52 +08:00
Support single file upload
This commit is contained in:
40
dist/index.js
vendored
40
dist/index.js
vendored
@@ -1909,6 +1909,7 @@ const METHOD_POST = 'POST'
|
||||
* @param {{ baseURL: string; timeout: number; headers: { [name: string]: string } }} param0.instanceConfig
|
||||
* @param {string} param0.data Request Body as string, default {}
|
||||
* @param {string} param0.files Map of Request Files (name: absolute path) as JSON String, default: {}
|
||||
* @param {string} param0.file Single request file (absolute path)
|
||||
* @param {{ username: string; password: string }|undefined} param0.auth Optional HTTP Basic Auth
|
||||
* @param {*} param0.actions
|
||||
* @param {number[]} param0.ignoredCodes Prevent Action to fail if the API response with one of this StatusCodes
|
||||
@@ -1917,7 +1918,7 @@ const METHOD_POST = 'POST'
|
||||
*
|
||||
* @returns {void}
|
||||
*/
|
||||
const request = async({ method, instanceConfig, data, files, auth, actions, ignoredCodes, preventFailureOnNoResponse, escapeData }) => {
|
||||
const request = async({ method, instanceConfig, data, files, file, auth, actions, ignoredCodes, preventFailureOnNoResponse, escapeData }) => {
|
||||
try {
|
||||
if (escapeData) {
|
||||
data = data.replace(/"[^"]*"/g, (match) => {
|
||||
@@ -1930,8 +1931,8 @@ const request = async({ method, instanceConfig, data, files, auth, actions, igno
|
||||
}
|
||||
|
||||
if (files && files !== '{}') {
|
||||
filesJson = convertToJSON(files)
|
||||
dataJson = convertToJSON(data)
|
||||
let filesJson = convertToJSON(files)
|
||||
let dataJson = convertToJSON(data)
|
||||
|
||||
if (Object.keys(filesJson).length > 0) {
|
||||
try {
|
||||
@@ -1944,6 +1945,12 @@ const request = async({ method, instanceConfig, data, files, auth, actions, igno
|
||||
}
|
||||
}
|
||||
|
||||
// Only consider file if neither data nor files provided
|
||||
if ((!data || data === '{}') && (!files || files === '{}') && file) {
|
||||
data = fs.createReadStream(file)
|
||||
updateConfigForFile(instanceConfig, file, actions)
|
||||
}
|
||||
|
||||
const requestData = {
|
||||
auth,
|
||||
method,
|
||||
@@ -2041,6 +2048,30 @@ const updateConfig = async (instanceConfig, formData, actions) => {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param instanceConfig
|
||||
* @param filePath
|
||||
* @param {*} actions
|
||||
*
|
||||
* @returns {{ baseURL: string; timeout: number; headers: { [name: string]: string } }}
|
||||
*/
|
||||
const updateConfigForFile = (instanceConfig, filePath, actions) => {
|
||||
try {
|
||||
const { size } = fs.statSync(filePath)
|
||||
|
||||
return {
|
||||
...instanceConfig,
|
||||
headers: {
|
||||
...instanceConfig.headers,
|
||||
'Content-Length': size,
|
||||
'Content-Type': 'application/octet-stream'
|
||||
}
|
||||
}
|
||||
} catch(error) {
|
||||
actions.setFailed({ message: `Unable to read Content-Length: ${error.message}`, data, files })
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {FormData} formData
|
||||
*
|
||||
@@ -4898,6 +4929,7 @@ const instanceConfig = {
|
||||
|
||||
const data = core.getInput('data') || '{}';
|
||||
const files = core.getInput('files') || '{}';
|
||||
const file = core.getInput('file')
|
||||
const method = core.getInput('method') || METHOD_POST;
|
||||
const preventFailureOnNoResponse = core.getInput('preventFailureOnNoResponse') === 'true';
|
||||
const escapeData = core.getInput('escapeData') === 'true';
|
||||
@@ -4909,7 +4941,7 @@ if (typeof ignoreStatusCodes === 'string' && ignoreStatusCodes.length > 0) {
|
||||
ignoredCodes = ignoreStatusCodes.split(',').map(statusCode => parseInt(statusCode.trim()))
|
||||
}
|
||||
|
||||
request({ data, method, instanceConfig, auth, preventFailureOnNoResponse, escapeData, files, ignoredCodes, actions: new GithubActions() })
|
||||
request({ data, method, instanceConfig, auth, preventFailureOnNoResponse, escapeData, files, file, ignoredCodes, actions: new GithubActions() })
|
||||
|
||||
|
||||
/***/ }),
|
||||
|
||||
Reference in New Issue
Block a user