Can use JSON Patch to add to entry reference?

I’m trying to use a patch call to add an entry reference and keep getting 415 errors, wondering if this is allowed? I’ve tried inserting before first index and at end (code below):

Here’s the body of the patch call i’m sending, anyone see something dumb i’m doing wrong?
[ { "op": "add", "path": "/fields/objects/en-US/-", "value": { "sys": { "type": "Link", "linkType": "Entry", "id": "bc0bf2a8d9e04d7a" } } } ]

Hi @niels ,
which API endpoint or tutorial are you using? I would like to verify and eventually replicate the error :slightly_smiling_face:

Was using this one: Content Management API | Contentful

Thank you!

For anyone else who runs into this issue. The problem was that when I was running a patch command to a given path for an add op, the issue was that I couldn’t add a the reference item in the case that no reference had previously existed.

So for my example above, the entry object didn’t yet have an objects field on it, therefore there was nothing to add a reference to. I had to create logic that first checked for the existence of the field and if it didn’t exist, conducted an add for the fully formed object / reference item vs. just adding the one object to it.

1 Like

Hi ! I’m curious to know the format you use to add a fully formed object to the item ? I’m running into the same issue and I am blocked at this point.
Thanks !

1 Like

Pretty sure it should be something like:

{ 
    [
        {
            "op": "add",
            "path": "/fields/{fieldName}",
            "value": {
                "en-US": [
                    {
                        "sys": {
                            "type": "Link",
                            "linkType": "Entry",
                            "id": "37e9c2f9ef994cb5"
                        }
                    }
                ]
            }
        }
    ]
}

live saving, thank you ! it works perfectly, I think it was the only structure I did not test !
Thanks again !

1 Like