mirror of
https://github.com/fjogeleit/http-request-action.git
synced 2024-12-23 12:46:14 +00:00
39 lines
993 B
JavaScript
39 lines
993 B
JavaScript
|
const core = require("@actions/core");
|
||
|
const axios = require("axios");
|
||
|
|
||
|
const auth = {}
|
||
|
const headers = { 'Content-Type': core.getInput('contentType') || 'application/json' }
|
||
|
|
||
|
if (!!core.getInput('username')) {
|
||
|
auth.username = core.getInput('username');
|
||
|
}
|
||
|
|
||
|
if (!!core.getInput('password')) {
|
||
|
auth.password = core.getInput('password');
|
||
|
}
|
||
|
|
||
|
if (!!core.getInput('bearerToken')) {
|
||
|
headers['Authentication'] = `Bearer ${core.getInput('bearerToken')}`;
|
||
|
}
|
||
|
|
||
|
const instance = axios.create({
|
||
|
baseURL: core.getInput('url', { required: true }),
|
||
|
timeout: parseInt(core.getInput('timeout') || 5000, 10),
|
||
|
headers
|
||
|
});
|
||
|
|
||
|
|
||
|
(async() => {
|
||
|
try {
|
||
|
const response = await instance.request({
|
||
|
auth,
|
||
|
method: core.getInput('method') || 'POST',
|
||
|
data: JSON.parse(core.getInput('data') || '{}')
|
||
|
})
|
||
|
|
||
|
core.setOutput('response', JSON.stringify(response.data))
|
||
|
} catch (error) {
|
||
|
core.setFailed(JSON.stringify({ code: error.response.code, message: error.response.data }))
|
||
|
}
|
||
|
})()
|