Java HTTP POST request to Airtable API failing

I’m having trouble converting this curl command to Java code:

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

I keep getting this error response:

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

Here’s my Java implementation:

String payload = "{" + 
    "records" + ":" + "[{" +
      "fields" + ":" + "{" +
        "Category" + ":" + "item_value" +
        "Status" + ":" + "Complete" +
      "}" +
    "}]," +
    "typecast" + ":" + "true" + 
"}";

public void insertRecord() 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());
}

I can’t figure out what’s wrong with my request format. The JSON looks correct to me but Airtable keeps rejecting it.

Your manual JSON construction is creating malformed JSON. When you concatenate strings like “Category” + “:” + “item_value”, you’re missing the double quotes that JSON requires. I hit this exact same issue last year and wasted hours debugging before realizing the problem wasn’t the API - it was my JSON format. Print your payload variable before sending the request and you’ll see it’s not valid JSON. Your current string creates something like {records:[{fields:{Category:item_value}}]} when it should be {“records”:[{“fields”:{“Category”:[“item_value”]}}]}. See how Category needs to be an array with square brackets, not a plain string? Missing commas between fields will also break parsing. Easiest fix is using a proper JSON library like org.json, or just fix your string building with proper escaping and structure.

Been there, done that. Manual JSON building is a nightmare and you’ll keep hitting parsing errors.

I dealt with this exact Airtable integration at my last company. Your string concatenation creates invalid JSON, but there’s a bigger issue - you’re overcomplicating this.

Ditch the manual string building. Use a Map structure:

Map<String, Object> fields = new HashMap<>();
fields.put("Category", Arrays.asList("Item1"));  // Note the array!
fields.put("Status", "Complete");

Map<String, Object> record = new HashMap<>();
record.put("fields", fields);

Map<String, Object> payload = new HashMap<>();
payload.put("records", Arrays.asList(record));
payload.put("typecast", true);

Let Unirest handle the JSON conversion automatically. Your Category field needs to be an array, not a string - that’s another reason it’s failing.

This saved me countless debugging hours. No more quote escaping headaches or malformed JSON.

Your JSON is broken - you’re missing quotes around keys and string values. JSON needs double quotes around everything, and you’re building it manually which causes these issues. Use Jackson or Gson instead of string concatenation. Here’s the fix: String payload = “{"records": [{"fields": {"Category": ["” + item_value + “"],"Status": "Complete"}}}],"typecast": true}”; See how Category needs square brackets for the array? And all strings need proper escaping. Manual string building is a pain and breaks easily. Switch to a JSON library for anything complex. Also check that item_value doesn’t have characters that’ll mess up your JSON.

you’re missing quotes all over the place, and there’s no comma between the Status and Category fields. also, Category needs to be an array like [“item_value”] instead of just “item_value”. print your payload string first - I bet it doesn’t look like valid json at all.