mirror of
https://github.com/fjogeleit/http-request-action.git
synced 2024-12-23 04:37:28 +00:00
Support for unescaped newlines in data (#16)
* support newline escaping
This commit is contained in:
parent
81e44c2059
commit
0cc64a5579
6 changed files with 36 additions and 6 deletions
12
.github/workflows/test.yml
vendored
12
.github/workflows/test.yml
vendored
|
@ -23,6 +23,18 @@ jobs:
|
|||
method: 'POST'
|
||||
data: '{ "key": "value" }'
|
||||
|
||||
- name: Request Postment Echo POST
|
||||
uses: ./
|
||||
with:
|
||||
url: 'https://postman-echo.com/post'
|
||||
method: 'POST'
|
||||
escapeData: 'true'
|
||||
data: >-
|
||||
{
|
||||
"key":"multi line\ntest
|
||||
text"
|
||||
}
|
||||
|
||||
- name: Request Postment Echo BasicAuth
|
||||
uses: ./
|
||||
with:
|
||||
|
|
|
@ -29,6 +29,7 @@ jobs:
|
|||
|bearerToken| Bearer Authentication Token (without Bearer Prefix) ||
|
||||
|customHeaders| Additional header values as JSON string, keys in this object overwrite default headers like Content-Type |'{}'|
|
||||
|preventFailureOnNoResponse| Prevent this Action to fail if the request respond without an response. Use 'true' (string) as value to enable it ||
|
||||
|escapeData| Escape newlines in data string content. Use 'true' (string) as value to enable it ||
|
||||
|
||||
### Output
|
||||
|
||||
|
|
|
@ -35,6 +35,9 @@ inputs:
|
|||
preventFailureOnNoResponse:
|
||||
description: 'Prevent this Action to fail if the request respond without an response'
|
||||
required: false
|
||||
escapeData:
|
||||
description: 'Escape newlines in data string content'
|
||||
required: false
|
||||
outputs:
|
||||
response:
|
||||
description: 'HTTP Response Content'
|
||||
|
|
13
dist/index.js
vendored
13
dist/index.js
vendored
|
@ -1023,10 +1023,16 @@ const axios = __webpack_require__(53);
|
|||
const METHOD_GET = 'GET'
|
||||
const METHOD_POST = 'POST'
|
||||
|
||||
const request = async({ method, instanceConfig, data, auth, actions, preventFailureOnNoResponse }) => {
|
||||
const request = async({ method, instanceConfig, data, auth, actions, preventFailureOnNoResponse, escapeData }) => {
|
||||
try {
|
||||
const instance = axios.create(instanceConfig);
|
||||
|
||||
if (escapeData) {
|
||||
data = data.replace(/"[^"]*"/g, (match) => {
|
||||
return match.replace(/[\n\r]\s*/g, "\\n");
|
||||
});
|
||||
}
|
||||
|
||||
const jsonData = method === METHOD_GET ? undefined : JSON.parse(data)
|
||||
|
||||
const requestData = {
|
||||
|
@ -1052,7 +1058,7 @@ const request = async({ method, instanceConfig, data, auth, actions, preventFail
|
|||
} else if (error.request && preventFailureOnNoResponse) {
|
||||
actions.warning(JSON.stringify(error));
|
||||
} else {
|
||||
actions.setFailed(error.message);
|
||||
actions.setFailed(JSON.stringify({ message: error.message, data }));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -2612,8 +2618,9 @@ core.debug('Instance Configuration: ' + JSON.stringify(instanceConfig))
|
|||
const data = core.getInput('data') || '{}';
|
||||
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, actions: new GithubActions() })
|
||||
request({ data, method, instanceConfig, auth, preventFailureOnNoResponse, escapeData, actions: new GithubActions() })
|
||||
|
||||
|
||||
/***/ }),
|
||||
|
|
|
@ -3,10 +3,16 @@ const axios = require("axios");
|
|||
const METHOD_GET = 'GET'
|
||||
const METHOD_POST = 'POST'
|
||||
|
||||
const request = async({ method, instanceConfig, data, auth, actions, preventFailureOnNoResponse }) => {
|
||||
const request = async({ method, instanceConfig, data, auth, actions, preventFailureOnNoResponse, escapeData }) => {
|
||||
try {
|
||||
const instance = axios.create(instanceConfig);
|
||||
|
||||
if (escapeData) {
|
||||
data = data.replace(/"[^"]*"/g, (match) => {
|
||||
return match.replace(/[\n\r]\s*/g, "\\n");
|
||||
});
|
||||
}
|
||||
|
||||
const jsonData = method === METHOD_GET ? undefined : JSON.parse(data)
|
||||
|
||||
const requestData = {
|
||||
|
@ -32,7 +38,7 @@ const request = async({ method, instanceConfig, data, auth, actions, preventFail
|
|||
} else if (error.request && preventFailureOnNoResponse) {
|
||||
actions.warning(JSON.stringify(error));
|
||||
} else {
|
||||
actions.setFailed(error.message);
|
||||
actions.setFailed(JSON.stringify({ message: error.message, data }));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -39,5 +39,6 @@ core.debug('Instance Configuration: ' + JSON.stringify(instanceConfig))
|
|||
const data = core.getInput('data') || '{}';
|
||||
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, actions: new GithubActions() })
|
||||
request({ data, method, instanceConfig, auth, preventFailureOnNoResponse, escapeData, actions: new GithubActions() })
|
||||
|
|
Loading…
Reference in a new issue