When creating a new POST request within the Safari console using the following example,
var request = new Request('/', {
method: 'POST',
body: '{ "foo": "1", "bar": 2 }'
});
request.json().then(function(json) {
console.log(json.foo);
console.log(json.bar);
});
However in my application when i try to access the request.text(), theres an error that states "NotSupportedError: The operation is not supported" and I'm not sure whats causing this to occur.
function sendRequest(input,requestInit){
// create request object for POST request
var request = new Request(input.url, requestInit);
// trying to read the clone body will throw the above error
Var requestclone = request.clone().text().then(text=>console.log(text)).catch(err=>console.log(err));
fetch(request).then(response=>{
//do stuff here
});
When running the same function with a GET or a PUT request, the .text() functions works and I run into no issues. The not supported Error only occurs on a POST request.
Any insight would be greatly appreciated.
edit1:
Running the following example in Safari and Chrome results in differrent results when the body key is a formData object.
var formDataObj = new FormData();
formDataObj.append('key','value')
var requestOptions = {
cache: "default",
credentials: "same-origin",
method: "POST",
mode: "cors"
}
var requestOptionsString = {
body: "text",
cache: "default",
credentials: "same-origin",
method: "POST",
mode: "cors"
}
var requestOptionsJSON = {
body: {"key":"value"},
cache: "default",
credentials: "same-origin",
method: "POST",
mode: "cors"
}
var requestOptionsFD = {
body: formDataObj,
cache: "default",
credentials: "same-origin",
method: "POST",
mode: "cors"
}
function requestTest(input,options){
var request = new Request(input,options)
request.clone().formData().then(formData=>console.log("formData",input,formData)).catch(err=>console.log("noFormData",input,err))
request.clone().json().then(json=>console.log("json",input,JSON.stringify(json))).catch(err=>console.log("noJson",input,err))
request.clone().text().then(text=>console.log("text",input,text)).catch(err=>console.log("noText",input,err))
request.clone().arrayBuffer().then(arrayBuffer=>console.log("arrayBuffer",input,arrayBuffer)).catch(err=>console.log("noArrayBuffer",input,err))
request.clone().blob().then(blob=>console.log("blob",input,blob)).catch(err=>console.log("noBlob",input,err))
fetch(request).then(response=>console.log("response",input,response)).catch(error=>console.log("error",input,error))
}
requestTest('none',requestOptions)
requestTest('string',requestOptionsString)
requestTest('json',requestOptionsJSON)
requestTest('formData',requestOptionsFD)