Help

Re: PATCH request not working

3124 2
cancel
Showing results for 
Search instead for 
Did you mean: 
rebal15
6 - Interface Innovator
6 - Interface Innovator

I’ve got a cURL PATCH request in order to update a record, however I seem to be getting this error:

{
  "error": {
    "type": "INVALID_REQUEST_MISSING_FIELDS",
    "message": "Could not find field \"fields\" in the request body"
  }
}

This is my current cURL request, I am using PHP:

<?php

// get cURL resource
$ch = curl_init();

// set url
curl_setopt($ch, CURLOPT_URL, 'https://api.airtable.com/v0/appmAdTUWbbY2vitV/Members/recl0qsFKY2dkR1NR');

// set method
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PATCH');

// return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

// set headers
curl_setopt($ch, CURLOPT_HTTPHEADER, [
  'Content-Type: application/json; charset=utf-8',
  'Cookie: brw=brwjOwXWWJ95PfnJr',
]);

// json body
$json_array = [
  'records' => [
    [
      'id' => 'recl0qsFKY2dkR1NR',
      'fields' => [
        'Rank' => 'Captain'
      ]
    ]
  ]
]; 
$body = json_encode($json_array);

// set body
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);

// send the request and save response to $response
$response = curl_exec($ch);

// stop if fails
if (!$response) {
  die('Error: "' . curl_error($ch) . '" - Code: ' . curl_errno($ch));
}

echo 'HTTP Status Code: ' . curl_getinfo($ch, CURLINFO_HTTP_CODE) . PHP_EOL;
echo 'Response Body: ' . $response . PHP_EOL;

// close curl resource to free up system resources 
curl_close($ch);

Note, I’ve deliberately left out my API key.

I’ve tried searching the web, but nothing has yet worked! Please help! I do not know how to use the bundled .js API, which is why I am doing it this way.

8 Replies 8

Just a hunch - I don’t think this should be an array; rather, an object like this which contains an array of records:

image

Thanks for the help, I got it working. The reason was that I did not need the “records” index.

It also looks like you are doing a POST instead of a PATCH.

curl_setopt($ch, CURLOPT_POST, 1);

Yep I completely rewrote the code.

If you are making a PATCH request to the record endpoint url, you don’t use the “records” level of the multi-dimensional array in the $body.

If you are making a PATCH request to the table endpoint url, you do need the “records” level of the multi-dimensional array in the $body.

You were mixing and matching the record endpoint (for updating a single record) with the format for updating multiple records (which should be sent to the table endpoint).

The syntax of the $body was fine. The PHP syntax looks a little strange to if you’re used to JavaScript, but it should work.

Ahh I see. Thanks so much for clarifying that.

Meu_Porteiro_Ap
4 - Data Explorer
4 - Data Explorer

Hi @rebal15 , may you share your final code with me please, I have similiar problems and believe you already solved with this discussion.

O_G
4 - Data Explorer
4 - Data Explorer

Hey @rebal15 , can you update us with your final code? I cannot get a patch request to work for the life of me.