Page 1 of 1

How to set custom field values when creating new page via API ?

Posted: Sun Sep 12, 2021 6:43 am
by metamouse
Using the API, how do I pass the custom field values when creating a new page? Please I'd appreciate some help with this. Just need to know if it's even possible and how the format is to send in the curl post.
Example of new page fields via api.

Code: Select all

$fields = array(
            'token' => "ahblah",
            'authentication' => "blahblah",
            'title' => $szz['title'],
            'content' => $szz['description'],
            'slug' => $szz['slug'],
            'coverImage' => $mainimg,
            'tags' => $tagsz,
            'custom' => array (
      'boxart' => $szz['boxart'],
      'horizontalHeaderImage' => $szz['horizontalHeaderImage']
    )
        );
$fields_string = http_build_query($fields);
Doesn't work, can someone show me how to send the custom array along with the new page data?

Re: How to set custom field values when creating new page via API ?

Posted: Sun Sep 12, 2021 1:04 pm
by diego
Hi,
yes you can send the custom field through the API, but first, you need to define the custom fields in Admin panel > Settings > Custom fields.

Here is the part of the code for the page creation and the custom fields.
https://github.com/bludit/bludit/blob/m ... hp#L68-L78

Regards
Diego

Re: How to set custom field values when creating new page via API ?

Posted: Sun Sep 12, 2021 3:25 pm
by metamouse
diego wrote: Sun Sep 12, 2021 1:04 pm Hi,
yes you can send the custom field through the API, but first, you need to define the custom fields in Admin panel > Settings > Custom fields.

Here is the part of the code for the page creation and the custom fields.
https://github.com/bludit/bludit/blob/m ... hp#L68-L78

Regards
Diego
Hey thanks for replying, I already read over that part of the code and still don't know how to send the data correctly. Can you please show me how to pass the data?

I already have defined the custom fields and they show up correctly in the admin panel.

Re: How to set custom field values when creating new page via API ?

Posted: Sun Sep 12, 2021 4:44 pm
by diego
I don't know your code, I mean how do you encode to JSON, I can show you an example I just did with curl command.

Admin panel > Settings > Custom field

Code: Select all

{
    "subtitle": {
        "type": "string",
        "placeholder": "Subtitle for the page"
    }
}
Curl to the API with the custom subtitle

Code: Select all

curl 'http://localhost/api/pages' -H 'content-type: application/json' --data-raw '{"token":"xxxxxxxxxxxxxxx","authentication":"xxxxxxxxxxx", "title":"My new page", "custom":{"subtitle":"Movie time"}}

Re: How to set custom field values when creating new page via API ?

Posted: Sun Sep 12, 2021 4:48 pm
by diego
Notice to create a page via API you need to use the method POST and send the parameters via POST.

Please follow the documentation example and then add the custom field.

https://docs.bludit.com/en/api/create-a-new-page

Re: How to set custom field values when creating new page via API ?

Posted: Sun Sep 12, 2021 5:00 pm
by metamouse
diego wrote: Sun Sep 12, 2021 4:44 pm I don't know your code, I mean how do you encode to JSON, I can show you an example I just did with curl command.

Admin panel > Settings > Custom field

Code: Select all

{
    "subtitle": {
        "type": "string",
        "placeholder": "Subtitle for the page"
    }
}
Curl to the API with the custom subtitle

Code: Select all

curl 'http://localhost/api/pages' -H 'content-type: application/json' --data-raw '{"token":"xxxxxxxxxxxxxxx","authentication":"xxxxxxxxxxx", "title":"My new page", "custom":{"subtitle":"Movie time"}}
Exactly what I needed. Thanks a ton!