Java HTTP POST to Airtable API failing

I’m struggling to send data to Airtable using Java, and I keep running into a parsing error with my request.

I have this curl command that works:

curl -X POST \
  https://api.airtable.com/v0/BASE_KEY/TABLE_ID \
  -H 'Content-Type: application/json' \
  -d '{
    "records": [{
      "fields": {
        "Name": "Test Entry",
        "Status": "Active"
      }
    }]
  }'

However, my Java code is producing the following error:

{"type":"INVALID_REQUEST_BODY","message":"Could not parse request body"}

Here is the code I am using:

String payload = "{" +
    "records" + ":" + "[{" +
    "fields" + ":" + "{" +
    "Name" + ":" + "testValue" + "," +
    "Status" + ":" + "pending" +
    "}" + "}]" +
"}";

public void addRecord() throws UnirestException {
    HttpResponse<JsonNode> result = Unirest.post("https://api.airtable.com/v0/" + config.BASE_ID + "/" + config.TABLE_ID)
        .header("Content-Type", "application/json")
        .header("Authorization", "Bearer " + config.API_TOKEN)
        .body(payload)
        .asJson();
    System.out.println(result.getBody().toString());
}

What am I doing wrong? The structure looks fine to me, but something must be off with the JSON format.

you’re missing quotes around the field names and values in your java string. try "records":[{"fields":{"Name":"testValue","Status":"pending"}}] instead. what you’ve got now creates invalid json because it’s not properly quoted.

This topic was automatically closed 4 days after the last reply. New replies are no longer allowed.