API - POST method from sending raw JSON to x-www-form-urlencoded

0 votes
76 views
added Mar 10 in API by lcjr Lieutenant (12,120 points)

RAW

// RAW
const response = await fetch("https://MYAPI_ENDPOINT/LEADS", {
  method: "POST",
  headers: {
    "Content-Type": "application/json",
  },
  body: JSON.stringify(payload),
});



// x-www-form-urlencoded
const formBody = new URLSearchParams();
for (const key in payload) {
  formBody.append(key, payload[key]);
}

const response = await fetch("https://MYAPI_ENDPOINT/LEADS", {
  method: "POST",
  headers: {
    "Content-Type": "application/x-www-form-urlencoded",
  },
  body: formBody.toString(),
});

 

lazacode.org - Malaysia's programming knowledge sharing platform, where everyone can share their finding as reference to others.
...