May 13, 2021 11:52 PM
Any advice on getting a PATCH request through PHP to work? I’ve tried editing just about every line of the following code (API key, base key, and row ID omitted). PHP doesn’t give any errors, but the ID won’t update.
$ch = curl_init();
$api_key = ''; $base = ''; $table = $_GET['game_id']; $url = 'https://api.airtable.com/v0/' . $base . '/' . $table; $headers = array( 'Content-Type: application/json', 'Authorization: Bearer ' . $api_key, ); // json body $json_array = [ [ 'id' => '', 'fields' => [ 'coordinate' => 'test' ] ] ]; $body = json_encode($json_array); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PATCH'); curl_setopt($ch, CURLOPT_POSTFIELDS, $body); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); $r = curl_exec($ch); curl_close($ch);
May 14, 2021 12:31 AM
May 14, 2021 07:58 AM
I don’t see anything obvious, but it’s been a while since I’ve written in PHP.
Here are some general troubleshooting/debugging suggestions.
Are you able to get and create records with the same API key, base ID, and table ID? If so, then they are not the issue.
For the record Id, are you using Airtable’s internal record id that begins with
rec followed by 14 characters? Your code does not show how you are getting the record ID.
Try echoing the response you get from the request. It may have some clues.
echo to see the progress of the script and the values used (since PHP doesn’t have the equivalent of console.log). If nothing echos, maybe you have a syntax error.
May 14, 2021 12:06 PM
This was very helpful, thanks so much for the debugging tips! I’m pretty new to PHP. Through an echo, I found the problem: the response string is returning error “INVALID_REQUEST_MISSING_FIELDS, could not find field “fields” in the request body”. Odd, but a good clue to go off of.
May 14, 2021 12:40 PM
Any advice on dealing with this error? Patch request: "Could not find field \"fields\" in the request body" error
May 14, 2021 03:41 PM
It sounds like Airtable does not like how you have formed the request body.
Double check with the REST API documentation on how to form your request body. Note that the request body (and the endpoint) varies slightly depending on if you are updating a single record versus a list of records. For example, it looks like with a list, you need to include a “records” level in the body.