request API /datasets/{dataset_id}/retrieve response 401 error #13080

Closed
opened 2026-02-21 19:10:29 -05:00 by yindo · 2 comments
Owner

Originally created by @AnoyiX on GitHub (Apr 17, 2025).

Self Checks

  • This is only for bug report, if you would like to ask a question, please head to Discussions.
  • I have searched for existing issues search for existing issues, including closed ones.
  • I confirm that I am using English to submit this report (我已阅读并同意 Language Policy).
  • [FOR CHINESE USERS] 请务必使用英文提交 Issue,否则会被关闭。谢谢!:)
  • Please do not modify this template :) and fill in all the required fields.

Dify version

v1.2.0

Cloud or Self Hosted

Cloud

Steps to reproduce

use curl to test:

curl --location --request POST 'https://api.dify.ai/v1/datasets/3638b5e8-63d4-4f63-89e8-d9dd8dd4bec5/retrieve' \
--header 'Authorization: Bearer ${my-api-token}'\
--header 'Content-Type: application/json'\
--data-raw '{
  "query": "test",
  "retrieval_model": {
      "search_method": "semantic_search",
      "reranking_enable": false,
      "reranking_mode": null,
      "reranking_model": {
          "reranking_provider_name": "",
          "reranking_model_name": ""
      },
      "weights": null,
      "top_k": 1,
      "score_threshold_enabled": false,
      "score_threshold": null
  }
}'

other datasets api is ok.

✔️ Expected Behavior

Return the correct result

Actual Behavior

bad response:

{"code": "unauthorized", "message": "Access token is invalid", "status": 401}
Originally created by @AnoyiX on GitHub (Apr 17, 2025). ### Self Checks - [x] This is only for bug report, if you would like to ask a question, please head to [Discussions](https://github.com/langgenius/dify/discussions/categories/general). - [x] I have searched for existing issues [search for existing issues](https://github.com/langgenius/dify/issues), including closed ones. - [x] I confirm that I am using English to submit this report (我已阅读并同意 [Language Policy](https://github.com/langgenius/dify/issues/1542)). - [x] [FOR CHINESE USERS] 请务必使用英文提交 Issue,否则会被关闭。谢谢!:) - [x] Please do not modify this template :) and fill in all the required fields. ### Dify version v1.2.0 ### Cloud or Self Hosted Cloud ### Steps to reproduce use curl to test: ``` curl --location --request POST 'https://api.dify.ai/v1/datasets/3638b5e8-63d4-4f63-89e8-d9dd8dd4bec5/retrieve' \ --header 'Authorization: Bearer ${my-api-token}'\ --header 'Content-Type: application/json'\ --data-raw '{ "query": "test", "retrieval_model": { "search_method": "semantic_search", "reranking_enable": false, "reranking_mode": null, "reranking_model": { "reranking_provider_name": "", "reranking_model_name": "" }, "weights": null, "top_k": 1, "score_threshold_enabled": false, "score_threshold": null } }' ``` other datasets api is ok. ### ✔️ Expected Behavior Return the correct result ### ❌ Actual Behavior bad response: ``` {"code": "unauthorized", "message": "Access token is invalid", "status": 401} ```
yindo closed this issue 2026-02-21 19:10:29 -05:00
Author
Owner

@dosubot[bot] commented on GitHub (Apr 17, 2025):

I found a similar unsolved discussion regarding a 401 unauthorized error: Access unauthorized 401 [1].

For the /datasets/{dataset_id}/retrieve API endpoint, ensure that you include the Authorization header with a Bearer token formatted correctly as Bearer {api_key} without any curly braces [2]. Additionally, verify that your API key is valid and that the CORS configuration is correct, with CONSOLE_CORS_ALLOW_ORIGINS and WEB_API_CORS_ALLOW_ORIGINS properly set, typically to '*' [3].

Make sure you have the necessary permissions to access the dataset. The user must be logged in, have an initialized account, and have the required dataset permissions. The user's current_tenant_id must match the dataset's tenant_id, and if the user's role is not TenantAccountRole.OWNER, additional checks are performed based on the dataset's permission settings [4][5].

To continue talking to Dosu, mention @dosu.


Help Dosu learn! Give it feedback: Great Response | Irrelevant Answer | Incorrect Sources | Too Verbose | Hallucination | Bug Report | Other

@dosubot[bot] commented on GitHub (Apr 17, 2025): <!-- Answer --> I found a similar unsolved discussion regarding a 401 unauthorized error: [Access unauthorized 401](https://github.com/langgenius/dify/discussions/9237) <sup>[[1]](https://github.com/langgenius/dify/discussions/9237)</sup>. For the `/datasets/{dataset_id}/retrieve` API endpoint, ensure that you include the `Authorization` header with a Bearer token formatted correctly as `Bearer {api_key}` without any curly braces <sup>[[2]](https://github.com/langgenius/dify-docs/blob/main/zh_CN/guides/knowledge-base/knowledge-and-documents-maintenance/maintain-dataset-via-api.md)</sup>. Additionally, verify that your API key is valid and that the CORS configuration is correct, with `CONSOLE_CORS_ALLOW_ORIGINS` and `WEB_API_CORS_ALLOW_ORIGINS` properly set, typically to '*' <sup>[[3]](https://github.com/langgenius/dify/issues/15706)</sup>. Make sure you have the necessary permissions to access the dataset. The user must be logged in, have an initialized account, and have the required dataset permissions. The user's `current_tenant_id` must match the dataset's `tenant_id`, and if the user's role is not `TenantAccountRole.OWNER`, additional checks are performed based on the dataset's permission settings <sup>[[4]](https://github.com/langgenius/dify/blob/main/api/controllers/console/datasets/datasets.py)</sup><sup>[[5]](https://github.com/langgenius/dify/blob/main/api/services/dataset_service.py)</sup>. <!-- Dosu Comment Footer --> *To continue talking to [Dosu](https://dosu.dev), mention @dosu.* --- <sup>Help Dosu learn! Give it feedback: [Great Response](https://app.dosu.dev/response-feedback/1adf9634-5a9b-403e-b68c-90b693148b7d?feedback_type=great_response) | [Irrelevant Answer](https://app.dosu.dev/response-feedback/1adf9634-5a9b-403e-b68c-90b693148b7d?feedback_type=irrelevant_answer) | [Incorrect Sources](https://app.dosu.dev/response-feedback/1adf9634-5a9b-403e-b68c-90b693148b7d?feedback_type=incorrect_sources) | [Too Verbose](https://app.dosu.dev/response-feedback/1adf9634-5a9b-403e-b68c-90b693148b7d?feedback_type=too_verbose) | [Hallucination](https://app.dosu.dev/response-feedback/1adf9634-5a9b-403e-b68c-90b693148b7d?feedback_type=hallucination) | [Bug Report](https://app.dosu.dev/response-feedback/1adf9634-5a9b-403e-b68c-90b693148b7d?feedback_type=bug_report) | [Other](https://app.dosu.dev/response-feedback/1adf9634-5a9b-403e-b68c-90b693148b7d?feedback_type=other)</sup>
Author
Owner

@crazywoola commented on GitHub (Apr 17, 2025):

Please check the request payload

curl --location --request POST 'https://api.dify.ai/v1/datasets/3638b5e8-63d4-4f63-89e8-d9dd8dd4bec5/retrieve' \
--header 'Authorization: Bearer KEY-HERE'\
--header 'Content-Type: application/json'\
--data-raw '{
  "query": "test",
  "retrieval_model": {
      "search_method": "semantic_search",
      "reranking_enable": false,
      "reranking_mode": null,
      "reranking_model": {
          "reranking_provider_name": "",
          "reranking_model_name": ""
      },
      "weights": null,
      "top_k": 1,
      "score_threshold_enabled": false,
      "score_threshold": null
  }
}'

Remove the ``{}in${my-api-token}` should resolve this error.

@crazywoola commented on GitHub (Apr 17, 2025): Please check the request payload ``` curl --location --request POST 'https://api.dify.ai/v1/datasets/3638b5e8-63d4-4f63-89e8-d9dd8dd4bec5/retrieve' \ --header 'Authorization: Bearer KEY-HERE'\ --header 'Content-Type: application/json'\ --data-raw '{ "query": "test", "retrieval_model": { "search_method": "semantic_search", "reranking_enable": false, "reranking_mode": null, "reranking_model": { "reranking_provider_name": "", "reranking_model_name": "" }, "weights": null, "top_k": 1, "score_threshold_enabled": false, "score_threshold": null } }' ``` Remove the ``{}` in `${my-api-token}` should resolve this error.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langgenius/dify#13080