Firefox "Using Fetch"

by George Obregon

JavaScript

var myData = `{
"UPSSecurity": {
"UsernameToken": {
"Username": "geo.obregon",
"Password": "l1fE1sg00d"
},
"ServiceAccessToken": {
"AccessLicenseNumber": "5D514B0BAAB6F5BC"
}
},
"XAVRequest": {
"Request": {
"RequestOption": "1",
"TransactionReference": {
"CustomerContext": "Your Customer Context"
}
},
"MaximumListSize": "10",
"AddressKeyFormat": {
"ConsigneeName": "Consignee Name",
"BuildingName": "Building Name",
"AddressLine": "12380 Morris Road",
"PoliticalDivision2": "Alpharetta",
"PoliticalDivision1": "GA",
"PostcodePrimaryLow": "30009",
"CountryCode": "US"
}
}
}`;

// Example POST method implementation:

postData(`https://onlinetools.ups.com/rest/XAV`, {
    myData
  })
  .then(data => console.log(JSON.stringify(data))) // JSON-string from `response.json()` call
  .catch(error => console.error(error));

function postData(url = ``, data = {}) {
  // Default options are marked with *
  return fetch(url, {
      method: "POST", // *GET, POST, PUT, DELETE, etc.
      mode: "no-cors", // no-cors, cors, *same-origin
      cache: "no-cache", // *default, no-cache, reload, force-cache, only-if-cached
      /*  credentials: "same-origin",  */ // include, same-origin, *omit
      headers: {
        "Content-Type": "application/json; charset=utf-8",
        // "Content-Type": "application/x-www-form-urlencoded",
      },
      redirect: "follow", // manual, *follow, error
      referrer: "no-referrer", // no-referrer, *client
      body: JSON.stringify(data), // body data type must match "Content-Type" header
    })
    .then(response => response.json()); // parses response to JSON
}