Skip to content

Commit

Permalink
Merge pull request #1478 from scireum/feature/jmu/SE-14122-postJson-p…
Browse files Browse the repository at this point in the history
…arameters

Support the use of URLSearchParams and String in sirius.postJson
  • Loading branch information
jmuscireum authored Nov 18, 2024
2 parents b2c3390 + e0efa38 commit 5babe94
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
25 changes: 18 additions & 7 deletions src/main/resources/default/assets/common/core.js.pasta
Original file line number Diff line number Diff line change
Expand Up @@ -258,19 +258,30 @@ sirius.getJSON = function (url, params) {

/**@
* Calls the given URL, posts the given params and retrieves the resulting JSON.
*
* @param url the URL to invoke
* @param params the parameters to send POST data (parameters with 'undefined' values are not sent to the server)
* @param [useSearchParams] if true, the parameters are sent as URLSearchParams instead of FormData
* <p>
* The parameters can either be specified as an object with key-value pairs (where the value is either a string or an
* array of strings), directly as a URLSearchParams, or as a query string.
*
* @param {RequestInfo|URL} url the URL to invoke
* @param {Object|string|URLSearchParams} params the parameters to send POST data (parameters with 'undefined' values
* are not sent to the server)
* @param {boolean} [useSearchParams] if true, the parameters are sent as URLSearchParams instead of FormData. If the
* parameters are specified as URLSearchParams or query string, this is ignored
* @returns {Promise<any>} the received JSON data
*/
sirius.postJson = function (url, params, useSearchParams) {
let body = sirius.convertObjectToFormData(params);
if (useSearchParams) {
let body;

if (typeof params === 'string' || params instanceof URLSearchParams) {
body = params;
} else if (useSearchParams) {
// Convert the body to URLSearchParams, this may be necessary when using problematic characters in field names
// that are converted/replaced by the multipart decoder of Netty.
body = new URLSearchParams(body);
body = new URLSearchParams(sirius.convertObjectToFormData(params));
} else {
body = sirius.convertObjectToFormData(params);
}

return fetch(url, {
method: "post",
body: body
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

___invoke("/assets/common/common-polyfills.js.pasta")
___include("/assets/common/libs/es6-promise/es6-promise.min.js")
___include("/assets/common/libs/fetch/fetch.umd.js")
___include("/assets/common/libs/url-search-params/url-search-params.js")
___include("/assets/common/libs/formdata/formdata.min.js")
___include("/assets/common/libs/fetch/fetch.umd.js")
___include("/assets/common/libs/remove/remove.js")
___include("/assets/common/libs/ie11CustomProperties/ie11CustomProperties.js")

0 comments on commit 5babe94

Please sign in to comment.