FriendsOfFlarum / pages

Create custom static pages in Flarum
https://discuss.flarum.org/d/18301
MIT License
23 stars 12 forks source link

Interacting with api using Flarum ApiKey causes 405 error #45

Closed QyInvoLing closed 2 years ago

QyInvoLing commented 2 years ago

Bug Report

Current Behavior I tried to directly interact with the api /api/pages with authorization of Flarum official api keys, which caused error.

Steps to Reproduce The node source code I used is here:

fetch('https://FORUMURL/api/pages/1', {
    method: 'POST',
    body: JSON.stringify({
        "data": {
            "type": "pages",
            "id": "1",
            "attributes": {
                    "title": "Donators",
                    "slug": "donate",
                    "content": "test test html",
                    "isHidden": false,
                    "isRestricted": false,
                    "isHtml": true
            }
        }
    }),
    headers: {
        'Content-Type': 'application/json',
        "Authorization": "Token MYTOKENADDED; userId=1",
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36"
    }
}).then(res => res.json()).then(json => console.log(json))

which caused an 405 error: { errors: [ { status: '405', code: 'method_not_allowed' } ] }

I've already used this api key before with official/extensions' api interfaces, which performs correctly. But when it comes into FofPages, error occurs.

Environment

Flarum core 1.4.1
PHP version: 8.1.7
MySQL version: 8.0.24
Loaded extensions: Core, date, libxml, openssl, pcre, sqlite3, zlib, bcmath, ctype, curl, dom, filter, ftp, gd, gettext, hash, iconv, intl, json, mbstring, SPL, session, pcntl, standard, mysqlnd, PDO, pdo_mysql, pdo_sqlite, Phar, posix, Reflection, mysqli, shmop, SimpleXML, soap, sockets, sodium, sysvsem, tokenizer, xml, xmlreader, xmlwriter, zip, fileinfo, exif, apcu, memcached, Zend OPcache
+----------------------------------------+-------------+--------+
| Flarum Extensions                      |             |        |
+----------------------------------------+-------------+--------+
| ID                                     | Version     | Commit |
+----------------------------------------+-------------+--------+
| flarum-flags                           | v1.4.0      |        |
| flarum-suspend                         | v1.4.0      |        |
| flarum-subscriptions                   | v1.4.0      |        |
| flarum-tags                            | v1.4.0      |        |
| flarum-approval                        | v1.4.0      |        |
| askvortsov-auto-moderator              | v0.1.2      |        |
| flarum-sticky                          | v1.4.0      |        |
| flarum-markdown                        | v1.4.0      |        |
| fof-user-directory                     | 1.2.3       |        |
| fof-follow-tags                        | 1.1.5       |        |
| fof-byobu                              | 1.1.7       |        |
| afrux-forum-widgets-core               | v0.1.7      |        |
| v17development-user-badges             | v1.1.0      |        |
| the-turk-stickiest                     | 3.0.1       |        |
| the-turk-flamoji                       | 1.0.4       |        |
| swaggymacro-only-starter               | 0.6.4       |        |
| nearata-internal-links-noreload        | v2.1.1      |        |
| nearata-cakeday                        | v2.0.0      |        |
| matteocontrini-imgur-upload            | v3.9.1      |        |
| kyrne-websocket                        | 3.3.0       |        |
| katosdev-signature                     | 1.2         |        |
| justoverclock-toastme                  | 0.1.1       |        |
| ianm-level-ranks                       | 1.1.0       |        |
| ianm-follow-users                      | 1.1.1       |        |
| fof-username-request                   | 1.0.1       |        |
| fof-user-bio                           | 1.1.0       |        |
| fof-upload                             | 1.2.3       |        |
| fof-profile-image-crop                 | 1.0.1       |        |
| fof-polls                              | 1.1.0       |        |
| fof-pages                              | 1.0.4       |        |
| fof-nightmode                          | 1.4.1       |        |
| fof-links                              | 1.1.1       |        |
| fof-ignore-users                       | 1.1.0       |        |
| fof-geoip                              | 1.0.1       |        |
| fof-formatting                         | 1.0.2       |        |
| fof-filter                             | 1.1.1       |        |
| fof-drafts                             | 1.1.2       |        |
| fof-doorman                            | 1.1.1       |        |
| fof-analytics                          | 1.1.0       |        |
| flarumtr-mobile-search                 | v1.2        |        |
| flarum-statistics                      | v1.4.1      |        |
| flarum-mentions                        | v1.4.0      |        |
| flarum-lock                            | v1.4.0      |        |
| flarum-likes                           | v1.4.0      |        |
| flarum-lang-english                    | v1.4.0      |        |
| flarum-lang-chinese-simplified         | v1.1.1.1220 |        |
| flarum-bbcode                          | v1.4.0      |        |
| ffans-clipboardjs                      | v1.0.0      |        |
| extiverse-mercury                      | 0.2.0       |        |
| ecnu-im-sticky-sidenav                 | v1.1.0      |        |
| davwheat-custom-sidenav-links          | 1.0.1       |        |
| darkle-fancybox                        | 1.1.2       |        |
| clarkwinkelmann-no-email-notifications | 1.0.0       |        |
| blomstra-sort-order-toggle             | 0.2.0       |        |
| blomstra-fontawesome                   | 0.1.4       |        |
| becod-backtotop                        | v1.1        |        |
| askvortsov-rich-text                   | v2.1.7      |        |
| askvortsov-pwa                         | v3.1.3      |        |
| askvortsov-moderator-warnings          | v0.6.1      |        |
| askvortsov-markdown-tables             | v1.2.1      |        |
| afrux-top-posters-widget               | v0.1.4      |        |
| afrux-news-widget                      | v0.1.1      |        |
| afrux-forum-stats-widget               | v0.1.1      |        |
+----------------------------------------+-------------+--------+
Base URL: https://michan.fun
Installation path: /www/wwwroot/michan
Queue driver: sync
Mail driver: smtp
Debug mode: off

Possible solution(s) Check if the api of this extension matches the standard of Flarum

clarkwinkelmann commented 2 years ago

What action are you trying to perform?

To create a new page, POST to /api/pages (without any ID)

To update a page, PATCH to /api/pages/<id>

This is the standard JSON:API spec https://jsonapi.org/format/#crud-updating which I believe is also closely followed by all Flarum core REST endpoints.

If your HTTP client doesn't support PATCH, you can use the X-HTTP-Method-Override: PATCH header with a POST request.

QyInvoLing commented 2 years ago

OK, after using patch, it works perfectly. Thank you!