知识库api批量上传 #30395
Replies: 1 comment 4 replies
-
|
Hi @shuming-dev! I'm Dosu and I’m helping the dify team. The 401 error happens because the API key you’re using (starting with The correct endpoints for uploading files to the knowledge base are:
Your request should be a
Example using Python import requests
url = "http://your-dify-server/v1/datasets/{dataset_id}/document/create-by-file"
headers = {"Authorization": "Bearer user-api-token"}
files = {"file": open("data/test.pdf", "rb")}
data = {
"data": '{"doc_form": "text_model", "indexing_technique": "high_quality"}'
}
response = requests.post(url, headers=headers, files=files, data=data)
print(response.text)Common pitfalls: expired tokens, incorrect endpoint, wrong token type, or malformed multipart requests. The API automatically sets user context from the token, so don’t include user ID/email in the payload [usage tips]. If you regenerate your token and use the correct endpoint and format, batch uploads should work [troubleshooting]. To reply, just mention @dosu. How did I do? Good | Irrelevant | Incorrect | Verbose | Hallucination | Report 🐛 | Other |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Self Checks
1. Is this request related to a challenge you're experiencing? Tell me about your story.
The pipeline knowledge base I built wants to upload documents in batches through the API, but now I encounter a 401 error. Dify is a locally deployed version 1.11.2, and the api_key is a string starting with "data" that was generated. The request body should also be correct. I want to know where the problem is. Theoretically, the key exists. I get a response when I retrieve the list of knowledge bases, but uploading doesn't work.
import requests
url = "http://xxx.46.xx.xxx:xxxx/v1/files/upload"
files = { "file": ("example-file", open("data/test.pdf", "rb")) }
payload = { "user": "admin@xxxx.cn" }
headers = {"Authorization": "Bearer dataset-VW0at8WN5kUMjfdR7Yl1GqYm"}
response = requests.post(url, data=payload, files=files, headers=headers)
print(response.text)
{"code":"unauthorized","message":"Access token is invalid","status":401}
2. Additional context or comments
No response
Beta Was this translation helpful? Give feedback.
All reactions