Skip to main content

422 Error with XMLhttprequest

  • July 21, 2020
  • 1 reply
  • 19 views

I am trying to use XML to get and update information from my Airtable. I am doing the following for POST to update a value but I keep getting error 422. I am doing a very similar process for GET and it works, but not for POST.

function httpPost(BaseID,tablename, APIKey){
    var xmlHttp = new XMLHttpRequest();
    var url = "https://api.airtable.com/v0/" + BaseID + "/" + tablename +"/";
    var propValue = {
        "id": "recIYykIMqJGcvNNi",
        "fields": {
          "Variables": "Test 1",
          "Value": "136"
        }
      }
  
xmlHttp.open('POST', url, false);
xmlHttp.setRequestHeader('Content-type','application/json');
xmlHttp.setRequestHeader('Authorization',"Bearer " + APIKey);

xmlHttp.onreadystatechange = function() {
if(xmlHttp.readyState == 4 && xmlHttp.status == 200) {
        console.log(xmlHttp.responseText);
    }
console.log(xmlHttp.status)
};
xmlHttp.send(JSON.stringify(propValue));

}

1 reply

Justin_Barrett
Forum|alt.badge.img+21

According to the API docs, POST is for creating records. When updating records, it should be either PATCH or PUT, depending on the situation. Review the API docs for more details.