Skip to content

Commit

Permalink
fix(api): allow override of content-type in update() when value is st…
Browse files Browse the repository at this point in the history
…ring
  • Loading branch information
Birkbjo committed Nov 16, 2021
1 parent 861d9f7 commit af2eccb
Showing 1 changed file with 18 additions and 12 deletions.
30 changes: 18 additions & 12 deletions src/api/Api.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,26 +209,32 @@ class Api {
*
* @returns {Promise.<*>} The response body.
*/
update(url, data, useMergeStrategy = false) {
// Since we are currently using PUT to save the full state back, we have to use mergeMode=REPLACE
// to clear out existing values
update(url, data, useMergeStrategy = false, options = {}) {
let payload = data
// Ensure that headers are defined and are treated without case sensitivity
options.headers = new Headers(options.headers || {})

if (data !== undefined) {
if (
!options.headers.has('Content-Type') &&
typeof payload === 'string'
) {
options.headers.set('Content-Type', 'text/plain')
} else {
payload = JSON.stringify(data)
}
}

const urlForUpdate =
useMergeStrategy === true
? `${url}?${getMergeStrategyParam()}`
: url
if (typeof data === 'string') {
return this.request(
'PUT',
getUrl(this.baseUrl, urlForUpdate),
String(data),
{ headers: new Headers({ 'Content-Type': 'text/plain' }) }
)
}

return this.request(
'PUT',
getUrl(this.baseUrl, urlForUpdate),
JSON.stringify(data)
payload,
options
)
}

Expand Down

0 comments on commit af2eccb

Please sign in to comment.