Post JSON data to an URL

by George Obregon

HTML

<div></div>

JavaScript

const $div = document.querySelector('div');

// If you don't need user and password, delete headers (we are just using it for that)
fetch('https://onlinetools.ups.com/rest/XAV', {
    headers: 'Content-Type:application/json',
    method: 'post',
    body: JSON.stringify({
      "UPSSecurity": {
        "UsernameToken": {
          "Username": "geo.obregon",
          "Password": "u"
        },
        "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"
        }
      }
    })
  })
  .then(response => {
    if (response.ok) $div.textContent = 'Ok' + response;
    else alert(response.statusText);
  });