Could we help you? Please click the banners. We are young and desperately need the money
Ever got tired from writing POST Requests? Wish it could be simpler? All without the use of any bloated libraries such as jQuery?
This nifty little helper-function here is all you need:
function qpost(t,e,o=!1){let c=!1;switch(typeof e){case"object":if(e instanceof FormData)break;for(const t of Object.keys(e))"object"==typeof e[t]&&(e[t]=JSON.stringify(e[t]));e=new URLSearchParams(e).toString(),c="application/x-www-form-urlencoded";break;case"string":let t=!0;try{JSON.parse(e)}catch{t=!1}t&&(c="application/json")}const r={};return c&&(r["Content-Type"]=c),o&&(Array.isArray(o)?r.Accept=o.join(", "):r.Accept=o),fetch(t,{method:"POST",headers:r,body:e})}
< 500 Characters | < 500 Bytes
qpost(url, data, accept?): Promise<Response>
Sends POST request with provided data to specified url via the Fetch API and returns a promise that resolves into a response. If defined, only allows responses of accepted MIME type/s.
(Promise<Response>) A promised response to the sent request.
qpost does not require you to research and provide the correct Content-Type, it does this for you. It also converts your data into a parameter string if needed. These aspects allow qpost to function with only two simple parameter inputs.
const Data = {
action: 'login',
email: login_email
}
fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
body: new URLSearchParams(Data).toString()
})
qpost(url, Data)
qpost automated:
qpost will automatically convert nested objects contained within your data into JSON strings. No need for you to further complicate the way you structure things.
const Data = {
action: 'save',
data: JSON.stringify({
a: 'foo',
b: 'bar',
c: 'hello',
d: 'world'
})
}
const Data = {
action: 'save',
data: {
a: 'foo',
b: 'bar',
c: 'hello',
d: 'world'
}
}
qpost automated: