1
0
Fork 0
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:
Frank Jogeleit 2020-10-07 18:29:38 +02:00 committed by GitHub
parent 81e44c2059
commit 0cc64a5579
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 36 additions and 6 deletions

View file

@ -23,6 +23,18 @@ jobs:
method: 'POST' method: 'POST'
data: '{ "key": "value" }' 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 - name: Request Postment Echo BasicAuth
uses: ./ uses: ./
with: with:

View file

@ -29,6 +29,7 @@ jobs:
|bearerToken| Bearer Authentication Token (without Bearer Prefix) || |bearerToken| Bearer Authentication Token (without Bearer Prefix) ||
|customHeaders| Additional header values as JSON string, keys in this object overwrite default headers like Content-Type |'{}'| |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 || |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 ### Output

View file

@ -35,6 +35,9 @@ inputs:
preventFailureOnNoResponse: preventFailureOnNoResponse:
description: 'Prevent this Action to fail if the request respond without an response' description: 'Prevent this Action to fail if the request respond without an response'
required: false required: false
escapeData:
description: 'Escape newlines in data string content'
required: false
outputs: outputs:
response: response:
description: 'HTTP Response Content' description: 'HTTP Response Content'

13
dist/index.js vendored
View file

@ -1023,10 +1023,16 @@ const axios = __webpack_require__(53);
const METHOD_GET = 'GET' const METHOD_GET = 'GET'
const METHOD_POST = 'POST' const METHOD_POST = 'POST'
const request = async({ method, instanceConfig, data, auth, actions, preventFailureOnNoResponse }) => { const request = async({ method, instanceConfig, data, auth, actions, preventFailureOnNoResponse, escapeData }) => {
try { try {
const instance = axios.create(instanceConfig); 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 jsonData = method === METHOD_GET ? undefined : JSON.parse(data)
const requestData = { const requestData = {
@ -1052,7 +1058,7 @@ const request = async({ method, instanceConfig, data, auth, actions, preventFail
} else if (error.request && preventFailureOnNoResponse) { } else if (error.request && preventFailureOnNoResponse) {
actions.warning(JSON.stringify(error)); actions.warning(JSON.stringify(error));
} else { } 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 data = core.getInput('data') || '{}';
const method = core.getInput('method') || METHOD_POST; const method = core.getInput('method') || METHOD_POST;
const preventFailureOnNoResponse = core.getInput('preventFailureOnNoResponse') === 'true'; 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() })
/***/ }), /***/ }),

View file

@ -3,10 +3,16 @@ const axios = require("axios");
const METHOD_GET = 'GET' const METHOD_GET = 'GET'
const METHOD_POST = 'POST' const METHOD_POST = 'POST'
const request = async({ method, instanceConfig, data, auth, actions, preventFailureOnNoResponse }) => { const request = async({ method, instanceConfig, data, auth, actions, preventFailureOnNoResponse, escapeData }) => {
try { try {
const instance = axios.create(instanceConfig); 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 jsonData = method === METHOD_GET ? undefined : JSON.parse(data)
const requestData = { const requestData = {
@ -32,7 +38,7 @@ const request = async({ method, instanceConfig, data, auth, actions, preventFail
} else if (error.request && preventFailureOnNoResponse) { } else if (error.request && preventFailureOnNoResponse) {
actions.warning(JSON.stringify(error)); actions.warning(JSON.stringify(error));
} else { } else {
actions.setFailed(error.message); actions.setFailed(JSON.stringify({ message: error.message, data }));
} }
} }
} }

View file

@ -39,5 +39,6 @@ core.debug('Instance Configuration: ' + JSON.stringify(instanceConfig))
const data = core.getInput('data') || '{}'; const data = core.getInput('data') || '{}';
const method = core.getInput('method') || METHOD_POST; const method = core.getInput('method') || METHOD_POST;
const preventFailureOnNoResponse = core.getInput('preventFailureOnNoResponse') === 'true'; 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() })