[API Request]: /api/search to return url to result #2508

Closed
opened 2026-02-05 04:21:58 +03:00 by OVERLORD · 3 comments
Owner

Originally created by @danpoltawski on GitHub (Nov 26, 2021).

API Endpoint or Feature

Thanks for adding /api/search endpoint :) One attribute that this endpoint doesn't return is a url which is directly linkable - it seems as an API consumer I need to do additional business logic/API calls to get a URL to be able to link to the result.

Use-Case

I have an external system which displays related bookstack results by querying on a tag, I want to just display a list of search results in the external system and directly link to them using a single API call (and ideally not get into the internal details of bookstack pages/chapters/shelves etc)

i.e.

 $resp = $client->get("https://bookstack/api/search?query=[Tagname={$tagvalue}]");
 $response = json_decode($resp->getBody());
 foreach  ($response->data as $result) {
        $o.="<li><a target='_blank' href='{$result->url}'>{$result->name}</a></li>\n";        
 }   

Additional context

I quickly hacked in this locally with the following patch - if this approach is valid, i'd be happy to update tests etc and prepare a pull request.

diff --git a/app/Http/Controllers/Api/SearchApiController.php b/app/Http/Controllers/Api/SearchApiController.php
index ba960b9d..0a9d2df1 100644
--- a/app/Http/Controllers/Api/SearchApiController.php
+++ b/app/Http/Controllers/Api/SearchApiController.php
@@ -52,9 +52,10 @@ class SearchApiController extends ApiController
                 'id', 'name', 'slug', 'book_id',
                 'chapter_id', 'draft', 'template',
                 'created_at', 'updated_at',
-                'tags', 'type',
+                'tags', 'type', 'url',
             ]);
             $result->setAttribute('type', $result->getType());
+            $result->setAttribute('url', $result->getURL());
         }
 
         return response()->json([
Originally created by @danpoltawski on GitHub (Nov 26, 2021). ### API Endpoint or Feature Thanks for adding `/api/search` endpoint :) One attribute that this endpoint doesn't return is a url which is directly linkable - it seems as an API consumer I need to do additional business logic/API calls to get a URL to be able to link to the result. ### Use-Case I have an external system which displays related bookstack results by querying on a tag, I want to just display a list of search results in the external system and directly link to them using a single API call (and ideally not get into the internal details of bookstack pages/chapters/shelves etc) i.e. ``` $resp = $client->get("https://bookstack/api/search?query=[Tagname={$tagvalue}]"); $response = json_decode($resp->getBody()); foreach ($response->data as $result) { $o.="<li><a target='_blank' href='{$result->url}'>{$result->name}</a></li>\n"; } ``` ### Additional context I quickly hacked in this locally with the following patch - if this approach is valid, i'd be happy to update tests etc and prepare a pull request. ``` diff --git a/app/Http/Controllers/Api/SearchApiController.php b/app/Http/Controllers/Api/SearchApiController.php index ba960b9d..0a9d2df1 100644 --- a/app/Http/Controllers/Api/SearchApiController.php +++ b/app/Http/Controllers/Api/SearchApiController.php @@ -52,9 +52,10 @@ class SearchApiController extends ApiController 'id', 'name', 'slug', 'book_id', 'chapter_id', 'draft', 'template', 'created_at', 'updated_at', - 'tags', 'type', + 'tags', 'type', 'url', ]); $result->setAttribute('type', $result->getType()); + $result->setAttribute('url', $result->getURL()); } return response()->json([ ```
OVERLORD added the 🔩 API Request label 2026-02-05 04:21:58 +03:00
Author
Owner

@ssddanbrown commented on GitHub (Nov 26, 2021):

Hi @danpoltawski,

I quickly hacked in this locally with the following patch - if this approach is valid, i'd be happy to update tests etc and prepare a pull request.

Yeah, That all looks good. A PR would be appreciated. Would also need to update the example located here:
https://github.com/BookStackApp/BookStack/blob/master/dev/api/responses/search-all.json

@ssddanbrown commented on GitHub (Nov 26, 2021): Hi @danpoltawski, > I quickly hacked in this locally with the following patch - if this approach is valid, i'd be happy to update tests etc and prepare a pull request. Yeah, That all looks good. A PR would be appreciated. Would also need to update the example located here: https://github.com/BookStackApp/BookStack/blob/master/dev/api/responses/search-all.json
Author
Owner

@ssddanbrown commented on GitHub (Dec 6, 2021):

Added this while working in #3096 as part of b22dd3cb88.
Will be part of the next patch release.

@ssddanbrown commented on GitHub (Dec 6, 2021): Added this while working in #3096 as part of b22dd3cb88335b362fdc5989e818829798d6658b. Will be part of the next patch release.
Author
Owner

@danpoltawski commented on GitHub (Dec 7, 2021):

Thanks 😊 (sorry I didn't get the pull request in time)

@danpoltawski commented on GitHub (Dec 7, 2021): Thanks 😊 (sorry I didn't get the pull request in time)
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/BookStack#2508