PATCH (update) a row via API

Hello.

I am struggling with the patch url format to update a row…

On the API for my database, it lists the following:

https://mysite.com/api/database/rows/table/10121/{row_id}/?user_field_names=true

I know my table ID is 10121 and the row id is 114. The field I want to update in that row is field_81781…

Can someone run me through the correct URL format to update field 81781 (which is a string) to a new value of “test” ?

I’ve tried something like:

https://mysite.com/api/database/rows/table/10121/114/?filter__field_81781__contains=test

I would appreciate any help in this matter.

Thanks folks!

2 Likes

You need to pass the data you want to update in the request body as JSON.

So, you would make a PATCH request to:

https://mysite.com/api/database/rows/table/10121/114/

with these headers:

Authorization: Token {YOUR_API_KEY}
Content-Type: application/json

and with this request body:

{
    "field_81781": "test"
}

Hope this helps!

1 Like

Thanks Jamac.

Because I’m executing this in node-red, is there yet a method to do this in one complete url?

Hey @TC2020 .

I never used node-red but I can’t imagine they won’t let you send PATCH requests without a body.

Here is a link that might help: How to send a post request with content - #2 by Steve-Mcl - General - Node-RED Forum

Hey Alex. That was exactly what I needed.

Thank you to you and Jamac for helping me out.

Tim.

Hey jamac.

If field_81781 was an array, how would this change the request body?

Thanks.

I tried something like… {“field_73544[0].id”:“4”} … and that didn’t work…

In this case, the current id is 1 for the first technician in the list… I want to change it to the 4th technician in the list… field_73544 is the technician array field …

The request body should be a JSON object containing all the fields you want to update. if you’re unfamiliar with JSON, this page on the mozilla docs is a great resource.

Also, you need to pass in the entire array of technicians, exactly in the order you want it, to update the field, because whatever data you pass in for the field will be set as that field’s value.

So, the request body should look something like:

{
    "field_81781": [
        "array_item_1",
        "array_item_2",
        "array_item_3"
    ]
}

I’m not sure what field type you’re working with so the array items for you may not be strings.

Thanks Jamac.

So just to take it back a step…

I’ve got a a column that is a multiple select listing all of my technicians… in that same select, there is a value called “unassigned”… so the multiple select values would be Jim, Tim, John, Kevin, Unassigned…

So essentially I am trying to create link that patches and changes the Technician selected…

So for example, lets say Row 1, Technician has a value of Unassigned…

Using the method you list above:

{
“field_81781”: “test”
}

Works if the value is not an array…

But if I want to change row 1 from Unassigned to Tim, what Json style would I use in this case…

I’m afraid I don’t follow your suggestion to list the whole array…

I appreciate your time.
Thanks again.
Tim/

So as Tim is the 2nd entry in the list, I tried the following, to no avai;…

{“field_81781”: [2, “Tim”]}

Also tried, as you suggest, the whole array… which would be…

{

    "field_73544": [
        {
            "id": 2,
            "value": "Tim"
        }
    ]
}

image

For a multiple select field, you need to pass in an array of the option IDs, as shown in this image.

So, if you want to set the option with ID 2 selected, you would pass in:

{
    “field_81781”: [
        2
    ]
}

and then if you want to also set the option with ID 3 selected, you would pass in:

{
    “field_81781”: [
        2,
        3
    ]
}

Thanks Jamac.

I’ve tried your suggestion and I just can’t get it to go… it’s like it doesn’t even think about making the change lol!

Gotta be something I’m missing… I do have to ask though… if you look at the photo I posted… where does “Technician” fit in within all this?

That’s the user field name (the name that shows up on Baserow). If you have the user_field_names query parameter set to true in your request, then you have to use those names instead of the names like field_81781.

I just realized, I think you might be confused about what the option IDs are. For a select field, the option IDs aren’t related to what order the options are in the list. The option IDs are listed in the database API documentation under the “Fields” section. it should look like this:


The numbers next to the options here are the IDs, which is what you need to pass in the API request.

Ah I know why… The technicians are in their own table and linked in to the main spreadsheet…

That said, I’m not sure if I can do what I’m trying to do now…

image

So if the technicians are listed in another table, can I refer to the other table to update the tech name?

IE

{
    "field_73544": [
        {
            "field_73534": "Tim"
        }
    ]
}

The technician table ID is in table 10120 and the tech names are field 73534 where “Tim” is one of the names… The above code doesn’t work so I’m just not sure if it is impossible or if the technician table needs to be mentioned in the JSON…

Got It!

{
    "Technician": [
        4
    ]
}

where 4 corresponds to the technician Tim in the other table!

Thanks for the help!

where 4 corresponds to the technician Tim in the other table!

Just to clarify for future reference. You were trying to update a link row field, not a multiple select field is that right?