Skip to content

Following the API docs but its not clear ...

Solved Customisation
  • I want some widget code to be able to make a topic post in certain circumstances
    I generated an API key under userID 1
    but Im unsure where to put this key, in some APIs it would go as a BearerID in the body, and sometimes it goes as a key concatenated to the endpoint…
    but its not specified in the docs exactly how to do it
    https://docs.nodebb.org/api/write/#tag/topics
    Also usually you would expect to have to supply a Method type as POST?
    The code is currently along these lines

    const bodyData={
    "cid": 1,
    "title": "Test topic",
    "content": "This is the test topic's content",
    "timestamp": 556084800000,
    "tags": [
    "test",
    "topic"
    ]
    }
    const key='c56.. //redcated
     fetch(
       `https://aignite.nodebb.org/api/v3/topics/', bodyData
    )
    .then(response => response.json())
    .then( data=> {....
    

    I wonder if you don’t have to supply the key if its done with an Authentication cookie, but the above code doesnt work.
    Perhaps the bodyData parameter is in wrong place?

  • @Panda Here’s how I’ve done it using basic CURL commands in PHP - you should be able to gain an understanding of structure at least.

    $ch = curl_init();
    
    curl_setopt($ch, CURLOPT_URL, 'http://localhost:5001/api/v3/topics');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, "title=api test post&content=the contents of the test post&cid=1&_uid=1");
    
    $headers = array();
    $headers[] = 'Authorization: Bearer d2b8602d-01a6-86ee-bbe4-0e016144be0c';
    $headers[] = 'Content-Type: application/x-www-form-urlencoded';
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    
    $result = curl_exec($ch);
    if (curl_errno($ch)) {
        echo 'Error:' . curl_error($ch);
    }
    curl_close($ch);
        
        }
        // Item has already been processed. Continue loop until count exhausted
        else {
            echo "Checking " .
                "\nLine item already processed - \033[33m[Ignored]\n\033[0m";
        }
    }
    
  • @phenomlab
    OK. The official docs are lacking in detail (in comparison to usual API example docs)
    They omit any mention of headers and BearerID - which it seems from your example are required.
    How did you figure out that combination? Did you see more extensive documentation?

    I will try and replicate the same with a JS fetch, if not I will make a topic on NodeBB Community

  • Update, adding those fields in …

    The API code doesn’t work from a widget, but from a stand-alone hosted code it gives this error
    Error with Permissions-Policy header: Origin trial controlled feature not enabled: ‘interest-cohort’.
    Is that like a CORS issue? Is there a fix for that?

  • Update: It was a CSRF token issue.
    I hadn’t even heard of one of those 😁

  • @Panda said in Following the API docs but its not clear ...:

    How did you figure out that combination? Did you see more extensive documentation?

    There’s documentation all over the Internet in relation to curl and not really “NodeBB” specific. It’s one of the headers and required for authentication.

    A search for curl on NodeBB also shows this in detail

    https://community.nodebb.org/search?term=Curl&in=titlesposts&matchWords=all&showAs=posts

  • @phenomlab
    Yes just searched
    Found some curl things on a PhP related post on NodeBB now …
    I’m quite surprise anyone was using Curl and PhP with NodeBB !

  • @Panda you’d be surprised. If you consider that you’d need to use the API to be able to populate a WordPress widget for example (which in turn would of course be PHP), taking this route is still immensely popular.

  • phenomlabundefined phenomlab has marked this topic as solved on

Did this solution help you?
Did you find the suggested solution useful? Why not buy me a coffee? It's a nice gesture, and a great way to show your appreciation 💗

  • Bug Report

    Solved Bugs
    43
    21 Votes
    43 Posts
    878 Views

    @crazycells yep. I get it! Good point.

  • 1 Votes
    10 Posts
    297 Views

    @Panda because there is no match for the DNS entry specified. The receiving web server parses the headers looking for a destination hostname to match, and anything the web server is unable to resolve will be sent back to the root.

  • Nodebb design

    Solved General
    2
    1 Votes
    2 Posts
    147 Views

    @Panda said in Nodebb design:

    One negative is not being so good for SEO as more Server side rendered forums, if web crawlers dont run the JS to read the forum.

    From recollection, Google and Bing have the capability to read and process JS, although it’s not in the same manner as a physical person will consume content on a page. It will be seen as plain text, but will be indexed. However, it’s important to note that Yandex and Baidu will not render JS, although seeing as Google has a 90% share of the content available on the web in terms of indexing, this isn’t something you’ll likely lose sleep over.

    @Panda said in Nodebb design:

    The “write api” is preferred for server-to-server interactions.

    This is mostly based around overall security - you won’t typically want a client machine changing database elements or altering data. This is why you have “client-side” which could be DOM manipulation etc, and “server-side” which performs more complex operations as it can communicate directly with the database whereas the client cannot (and if it can, then you have a serious security flaw). Reading from the API is perfectly acceptable on the client-side, but not being able to write.

    A paradigm here would be something like SNMP. This protocol exists as a UDP (UDP is very efficient, as it is “fire and forget” and does not wait for a response like TCP does) based service which reads performance data from a remote source, thus enabling an application to parse that data for use in a monitoring application. In all cases, SNMP access should be “RO” (Read Only) and not RW (Read Write). It is completely feasible to assume complete control over a firewall for example by having RW access to SNMP and then exposing it to the entire internet with a weak passphrase.

    You wouldn’t do it (at least, I hope you wouldn’t) and the same ethic applies to server-side rendering and the execution of commands.

  • 2 Votes
    26 Posts
    1k Views

    @Panda said in Interesting Widget code, but can’t fetch API:

    How did you drop that widget into the post there?
    I hadnt seen this BSgenerator anywhere on sudonix site, do you use it somewhere already?

    Yes, here

    https://sudonix.org/topic/414/corporate-bullshit-generator?_=1687774393044

    It’s not a “post” or “topic” in the common sense. It is actually a page in it’s own right and leverages nodebb-plugin-custom-pages. This in turn creates a new “route” which behaves like a page, meaning it is then exposed for widgets.

    @Panda said in Interesting Widget code, but can’t fetch API:

    Also can you explain more what you mean by calling the code externally. In my API call example, how would I go about doing that?

    By this, I mean create all the required code in an external JS file that is reachable by the NodeBB instance - so, in “public” for example - or in my case /public/js. The widget then “calls” that file and because it runs outside of the scope of NodeBB, you just need to return the values to the widget.

    Hope this makes sense?

  • Further Widgets question

    Solved Configure
    4
    1 Votes
    4 Posts
    151 Views

    @Panda category is for a category in its own, so for example, “fruit” whereas categories is the page that contains all categories as a list.

  • Chevron up before & after

    Solved Configure
    11
    4 Votes
    11 Posts
    382 Views

    @crazycells you are right 🙂 thank you.

  • 14 Votes
    69 Posts
    5k Views

    @phenomlab

    Seems to be better with some scaling fix for redis on redis.conf. I haven’t seen the message yet since the changes I made

    # I increase it to the value of /proc/sys/net/core/somaxconn tcp-backlog 4096 # I'm uncommenting because it can slow down Redis. Uncommented by default !!!!!!!!!!!!!!!!!!! #save 900 1 #save 300 10 #save 60 10000

    If you have other Redis optimizations. I take all your advice

    https://severalnines.com/blog/performance-tuning-redis/

  • 4 Votes
    5 Posts
    642 Views

    @phenomlab thanks 🙏