Java HTTP POST to Airtable API throwing parsing error

I’m trying to convert this curl request to Java code but running into issues:

curl -X POST \
  https://api.airtable.com/v0/BASE_ID/TABLE_NAME \
  -H 'Content-Type: application/json' \
  -d '{
    "fields": {
        "Category": ["Item1"]
    },
    "typecast": true 
}'

My Java implementation keeps failing with this error message:

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

Here’s what I wrote:

String requestBody = "{" + 
    "fields" + ":" + "{" +
      "Products" + ":" + "item_id" +
      "Status" + ":" + "Active" +
    "},"+
    "typecast" + ":" + "true" + 
"}";

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(requestBody)
  .asJson();
  System.out.println(result.getBody().toString());
}

Can’t figure out what’s wrong with my request format. Any suggestions?

Your JSON structure doesn’t match the curl example. In curl you’ve got “Category”: [“Item1”] showing an array, but your Java code has “Products” + “:” + “item_id” without quotes or brackets. You’re also missing quotes around string values and keys.

The API wants proper JSON - strings need quotes, arrays need square brackets. Try “fields”:{“Products”:[“item_id”],“Status”:“Active”} instead. And your typecast should be boolean true, not the string “true”.

Building JSON manually with strings is a pain, especially with nested stuff. You’ll keep running into formatting issues.

Had this exact problem last month with a different API. Your string building is missing quotes everywhere and doesn’t match your curl structure.

Your curl has “Category”: [“Item1”] but your Java code randomly switches to “Products” and “Status” fields that weren’t in the original request. You’re also concatenating without quotes around keys and values.

Here’s what works:

String requestBody = "{" +
    "\"fields\":{" +
        "\"Category\":[\"Item1\"]" +
    "}," +
    "\"typecast\":true" +
"}";

After dealing with this mess a few times though, I just use a JSON library now:

ObjectMapper mapper = new ObjectMapper();
ObjectNode root = mapper.createObjectNode();
ObjectNode fields = mapper.createObjectNode();
ArrayNode category = mapper.createArrayNode();
category.add("Item1");
fields.set("Category", category);
root.set("fields", fields);
root.put("typecast", true);
String requestBody = mapper.writeValueAsString(root);

Saves you from debugging quote escaping issues every time.

your string concatenation’s missing quotes around the values. use a json library like jackson or gson instead of building strings manually - it’s way less error-prone and handles escaping for you.

Your JSON format is completely broken. You’re concatenating strings without proper quotes, and your field structure doesn’t match your curl example at all. The curl shows “Category”: [“Item1”] but your Java code randomly uses Products and Status fields instead.

I’ve worked with Airtable API for two years and hit similar parsing errors early on. The API’s strict about JSON formatting. You’re missing quotes around keys and values, plus treating the array value as a plain string.

Quick fix: make your requestBody match the curl structure exactly with proper escaping. But honestly? After debugging these string concatenation nightmares multiple times, just switch to a JSON library like Jackson. It’ll save you hours. The parsing error usually means malformed JSON syntax, not API authentication issues.