Failed to upload {file} Network/HTTP Error: 404 Client Error: NOT FOUND for url: http://localhost/v1/datasets/{dataset_id}/documents/upload #14820

Closed
opened 2026-02-21 19:18:54 -05:00 by yindo · 3 comments
Owner

Originally created by @mengze1320 on GitHub (Jun 23, 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

1.4.3

Cloud or Self Hosted

Self Hosted (Docker)

Steps to reproduce

I want to upload files through Dify API
Here's the code:
DIFY_API_BASE = "http://localhost/v1"
DIFY_API_KEY = "dataset-tDU3h856ibff3dfyDTEdQ5Qa" # <--- 替换为您的 Dify API Key
DIFY_DATASET_ID = "17ff410c-8963-46bb-a457-78bf83ac5cb4" # <--- 替换为您的 Dify 知识库 ID

--- 本地文件路径配置 ---

存放 HTML 文件的目录,确保与您之前保存的目录一致

LOCAL_HTML_OUTPUT_DIR = r"C:\Users\detec\Desktop\knowledgebase"
METADATA_JSON_FILE = r"C:\Users\detec\Desktop\knowledgebase\articles_metadata2.json"

--- HTTP Headers for Dify API ---

DIFY_HEADERS = {
"Authorization": f"Bearer {DIFY_API_KEY}"}

def upload_document_to_dify(file_name, file_content, metadata):
"""
上传单个文档及其元数据到 Dify。
:param file_name: 文档在 Dify 中显示的文件名。
:param file_content: 文件的二进制内容(例如 HTML 字符串的 encode('utf-8'))。
:param metadata: 一个字典,包含要上传的自定义元数据。
"""
upload_url = f"{DIFY_API_BASE}/datasets/{DIFY_DATASET_ID}/documents/upload"

# Dify API 要求文件上传使用 multipart/form-data
# 'file' 键对应文件本身 (filename, content_bytes, content_type)
# 'data' 键对应一个 JSON 字符串,包含额外的参数和 doc_metadata
files_payload = {
    'file': (file_name, file_content, 'text/html'), # content_type for HTML
    'data': (None, json.dumps({"doc_metadata": metadata,  }), 'application/json')}

  try:
    print(f"  Attempting to upload: {file_name} with metadata: {metadata}")
    response = requests.post(upload_url, headers=DIFY_HEADERS, files=files_payload)

    # --- 关键调试行 ---
    print(f"  Received HTTP Status Code: {response.status_code}")
    print(f"  Received Raw Response Text: '{response.text}'")  # 注意这里我加了引号,防止空字符串看不见
    # --- 关键调试行 ---

    response.raise_for_status()  # 这行会在收到 4xx 或 5xx 状态码时引发 HTTPError

    # 只有当状态码为 200 且响应文本不为空时才尝试解析 JSON
    if response.status_code == 200 and response.text:
        print(f"  SUCCESS: Uploaded {file_name}. Response JSON: {response.json()}")
        return True
    elif response.status_code == 200 and not response.text:
        print(f"  SUCCESS: Uploaded {file_name}, but response body was empty (Status 200).")
        return True  # 如果状态码是200但响应为空,Dify可能也认为是成功了
    else:
        # 如果 raise_for_status() 没有捕获到,但状态码又不是 200 的情况
        print(f"  FAILURE: Uploaded {file_name} with non-200 status code. (Details above)")
        return False

except requests.exceptions.RequestException as e:
    # 捕获网络错误、连接拒绝、超时或由 raise_for_status() 抛出的 HTTPError
    print(f"  FAILURE: Failed to upload {file_name}. Network/HTTP Error: {e}")
    # 状态码和原始响应文本应该已经在上面的调试行中打印了
    return False
except json.JSONDecodeError as e:
    # 专门捕获 JSON 解析错误
    print(f"  FAILURE: JSON Decode Error for {file_name}. Error: {e}")
    # 原始响应文本和状态码也应该已打印
    return False
except Exception as e:
    print(f"  An unexpected general error occurred during upload for {file_name}: {e}")
    return False

✔️ Expected Behavior

Succeed uploading

Actual Behavior

Loaded 1205 articles metadata from C:\Users\detec\Desktop\knowledgebase\考试院数据0623\中考招考\articles_metadata2.json
Attempting to upload: ....html with metadata:
Received HTTP Status Code: 404
Received Raw Response Text: '<!doctype html>

<html lang=en>

Not Found

The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again.

' FAILURE: Failed to upload .html. Network/HTTP Error: 404 Client Error: NOT FOUND for url: http://localhost:80/v1/datasets/17ff410c-8963-46bb-a457-78bf83ac5cb4/documents/upload
Originally created by @mengze1320 on GitHub (Jun 23, 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 1.4.3 ### Cloud or Self Hosted Self Hosted (Docker) ### Steps to reproduce I want to upload files through Dify API Here's the code: DIFY_API_BASE = "http://localhost/v1" DIFY_API_KEY = "dataset-tDU3h856ibff3dfyDTEdQ5Qa" # <--- 替换为您的 Dify API Key DIFY_DATASET_ID = "17ff410c-8963-46bb-a457-78bf83ac5cb4" # <--- 替换为您的 Dify 知识库 ID # --- 本地文件路径配置 --- # 存放 HTML 文件的目录,确保与您之前保存的目录一致 LOCAL_HTML_OUTPUT_DIR = r"C:\Users\detec\Desktop\knowledgebase" METADATA_JSON_FILE = r"C:\Users\detec\Desktop\knowledgebase\articles_metadata2.json" # --- HTTP Headers for Dify API --- DIFY_HEADERS = { "Authorization": f"Bearer {DIFY_API_KEY}"} def upload_document_to_dify(file_name, file_content, metadata): """ 上传单个文档及其元数据到 Dify。 :param file_name: 文档在 Dify 中显示的文件名。 :param file_content: 文件的二进制内容(例如 HTML 字符串的 encode('utf-8'))。 :param metadata: 一个字典,包含要上传的自定义元数据。 """ upload_url = f"{DIFY_API_BASE}/datasets/{DIFY_DATASET_ID}/documents/upload" # Dify API 要求文件上传使用 multipart/form-data # 'file' 键对应文件本身 (filename, content_bytes, content_type) # 'data' 键对应一个 JSON 字符串,包含额外的参数和 doc_metadata files_payload = { 'file': (file_name, file_content, 'text/html'), # content_type for HTML 'data': (None, json.dumps({"doc_metadata": metadata, }), 'application/json')} try: print(f" Attempting to upload: {file_name} with metadata: {metadata}") response = requests.post(upload_url, headers=DIFY_HEADERS, files=files_payload) # --- 关键调试行 --- print(f" Received HTTP Status Code: {response.status_code}") print(f" Received Raw Response Text: '{response.text}'") # 注意这里我加了引号,防止空字符串看不见 # --- 关键调试行 --- response.raise_for_status() # 这行会在收到 4xx 或 5xx 状态码时引发 HTTPError # 只有当状态码为 200 且响应文本不为空时才尝试解析 JSON if response.status_code == 200 and response.text: print(f" SUCCESS: Uploaded {file_name}. Response JSON: {response.json()}") return True elif response.status_code == 200 and not response.text: print(f" SUCCESS: Uploaded {file_name}, but response body was empty (Status 200).") return True # 如果状态码是200但响应为空,Dify可能也认为是成功了 else: # 如果 raise_for_status() 没有捕获到,但状态码又不是 200 的情况 print(f" FAILURE: Uploaded {file_name} with non-200 status code. (Details above)") return False except requests.exceptions.RequestException as e: # 捕获网络错误、连接拒绝、超时或由 raise_for_status() 抛出的 HTTPError print(f" FAILURE: Failed to upload {file_name}. Network/HTTP Error: {e}") # 状态码和原始响应文本应该已经在上面的调试行中打印了 return False except json.JSONDecodeError as e: # 专门捕获 JSON 解析错误 print(f" FAILURE: JSON Decode Error for {file_name}. Error: {e}") # 原始响应文本和状态码也应该已打印 return False except Exception as e: print(f" An unexpected general error occurred during upload for {file_name}: {e}") return False ### ✔️ Expected Behavior Succeed uploading ### ❌ Actual Behavior Loaded 1205 articles metadata from C:\Users\detec\Desktop\knowledgebase\考试院数据0623\中考招考\articles_metadata2.json Attempting to upload: ....html with metadata: Received HTTP Status Code: 404 Received Raw Response Text: '<!doctype html> <html lang=en> <title>404 Not Found</title> <h1>Not Found</h1> <p>The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again.</p> ' FAILURE: Failed to upload .html. Network/HTTP Error: 404 Client Error: NOT FOUND for url: http://localhost:80/v1/datasets/17ff410c-8963-46bb-a457-78bf83ac5cb4/documents/upload
yindo added the 🐞 bug label 2026-02-21 19:18:54 -05:00
yindo closed this issue 2026-02-21 19:18:54 -05:00
Author
Owner

@dosubot[bot] commented on GitHub (Jun 23, 2025):

The 404 error happens because /v1/datasets/{dataset_id}/documents/upload is not a valid endpoint in Dify 1.4.3. The correct endpoint for uploading documents is /v1/datasets/{dataset_id}/documents using a POST request with multipart/form-data.

Recent changes also require a process_rule parameter in your upload payload, or the upload may fail (though usually not with a 404) source.

Here's how you can adjust your code:

upload_url = f"{DIFY_API_BASE}/datasets/{DIFY_DATASET_ID}/documents"
files_payload = {
    'file': (file_name, file_content, 'text/html'),
    'data': (None, json.dumps({
        "doc_metadata": metadata,
        "process_rule": "your_process_rule_here"  # Replace with your actual process rule
    }), 'application/json')
}
response = requests.post(upload_url, headers=DIFY_HEADERS, files=files_payload)

Make sure your Dify backend is running and accessible at the expected URL and port. If you still get a 404 at the correct endpoint, double-check your Docker setup and that the API service is healthy.

Let me know if you need more details on the process_rule or encounter a different error!

To reply, just mention @dosu.


How did I do? Good | Irrelevant | Incorrect | Verbose | Hallucination | Report 🐛 | Other  Join Discord Share on X

@dosubot[bot] commented on GitHub (Jun 23, 2025): <!-- Answer --> The 404 error happens because /v1/datasets/{dataset_id}/documents/upload is not a valid endpoint in Dify 1.4.3. The correct endpoint for uploading documents is /v1/datasets/{dataset_id}/documents using a POST request with multipart/form-data. Recent changes also require a process_rule parameter in your upload payload, or the upload may fail (though usually not with a 404) [source](https://github.com/langgenius/dify/pull/20599). Here's how you can adjust your code: ```python upload_url = f"{DIFY_API_BASE}/datasets/{DIFY_DATASET_ID}/documents" files_payload = { 'file': (file_name, file_content, 'text/html'), 'data': (None, json.dumps({ "doc_metadata": metadata, "process_rule": "your_process_rule_here" # Replace with your actual process rule }), 'application/json') } response = requests.post(upload_url, headers=DIFY_HEADERS, files=files_payload) ``` Make sure your Dify backend is running and accessible at the expected URL and port. If you still get a 404 at the correct endpoint, double-check your Docker setup and that the API service is healthy. Let me know if you need more details on the process_rule or encounter a different error! <!-- Dosu Comment Footer --> *To reply, just mention [@dosu](https://go.dosu.dev/dosubot).* --- <sup>How did I do? [Good](https://app.dosu.dev/response-feedback/7616054f-b1ec-4b6a-852b-d5b26939813c?feedback_type=great_response) | [Irrelevant](https://app.dosu.dev/response-feedback/7616054f-b1ec-4b6a-852b-d5b26939813c?feedback_type=irrelevant_answer) | [Incorrect](https://app.dosu.dev/response-feedback/7616054f-b1ec-4b6a-852b-d5b26939813c?feedback_type=incorrect_sources) | [Verbose](https://app.dosu.dev/response-feedback/7616054f-b1ec-4b6a-852b-d5b26939813c?feedback_type=too_verbose) | [Hallucination](https://app.dosu.dev/response-feedback/7616054f-b1ec-4b6a-852b-d5b26939813c?feedback_type=hallucination) | [Report 🐛](https://app.dosu.dev/response-feedback/7616054f-b1ec-4b6a-852b-d5b26939813c?feedback_type=bug_report) | [Other](https://app.dosu.dev/response-feedback/7616054f-b1ec-4b6a-852b-d5b26939813c?feedback_type=other)</sup>&nbsp;&nbsp;[![Join Discord](https://img.shields.io/badge/join-5865F2?logo=discord&logoColor=white&label=)](https://go.dosu.dev/discord-bot)&nbsp;[![Share on X](https://img.shields.io/badge/X-share-black)](https://twitter.com/intent/tweet?text=%40dosu_ai%20helped%20me%20solve%20this%20issue!&url=https%3A//github.com/langgenius/dify/issues/21388)
Author
Owner

@mengze1320 commented on GitHub (Jun 23, 2025):

The 404 error happens because /v1/datasets/{dataset_id}/documents/upload is not a valid endpoint in Dify 1.4.3. The correct endpoint for uploading documents is /v1/datasets/{dataset_id}/documents using a POST request with multipart/form-data.

Recent changes also require a process_rule parameter in your upload payload, or the upload may fail (though usually not with a 404) source.

Here's how you can adjust your code:

upload_url = f"{DIFY_API_BASE}/datasets/{DIFY_DATASET_ID}/documents"
files_payload = {
'file': (file_name, file_content, 'text/html'),
'data': (None, json.dumps({
"doc_metadata": metadata,
"process_rule": "your_process_rule_here" # Replace with your actual process rule
}), 'application/json')
}
response = requests.post(upload_url, headers=DIFY_HEADERS, files=files_payload)
Make sure your Dify backend is running and accessible at the expected URL and port. If you still get a 404 at the correct endpoint, double-check your Docker setup and that the API service is healthy.

Let me know if you need more details on the process_rule or encounter a different error!

To reply, just mention @dosu.

How did I do? Good | Irrelevant | Incorrect | Verbose | Hallucination | Report 🐛 | Other  Join Discord Share on X

The 404 error happens because /v1/datasets/{dataset_id}/documents/upload is not a valid endpoint in Dify 1.4.3. The correct endpoint for uploading documents is /v1/datasets/{dataset_id}/documents using a POST request with multipart/form-data.

Recent changes also require a process_rule parameter in your upload payload, or the upload may fail (though usually not with a 404) source.

Here's how you can adjust your code:

upload_url = f"{DIFY_API_BASE}/datasets/{DIFY_DATASET_ID}/documents"
files_payload = {
'file': (file_name, file_content, 'text/html'),
'data': (None, json.dumps({
"doc_metadata": metadata,
"process_rule": "your_process_rule_here" # Replace with your actual process rule
}), 'application/json')
}
response = requests.post(upload_url, headers=DIFY_HEADERS, files=files_payload)
Make sure your Dify backend is running and accessible at the expected URL and port. If you still get a 404 at the correct endpoint, double-check your Docker setup and that the API service is healthy.

Let me know if you need more details on the process_rule or encounter a different error!

To reply, just mention @dosu.

How did I do? Good | Irrelevant | Incorrect | Verbose | Hallucination | Report 🐛 | Other  Join Discord Share on X

Thanks. You are right!
But now I encountered a new issue:
Attempting to upload: .html with metadata:
Received HTTP Status Code: 405
Received Raw Response Text (first 500 chars): '{"code": "method_not_allowed", "message": "The method is not allowed for the requested URL.", "status": 405}
...'
FAILURE: Uploaded .html with non-success status code: 405
@dosu

@mengze1320 commented on GitHub (Jun 23, 2025): > The 404 error happens because /v1/datasets/{dataset_id}/documents/upload is not a valid endpoint in Dify 1.4.3. The correct endpoint for uploading documents is /v1/datasets/{dataset_id}/documents using a POST request with multipart/form-data. > > Recent changes also require a process_rule parameter in your upload payload, or the upload may fail (though usually not with a 404) [source](https://github.com/langgenius/dify/pull/20599). > > Here's how you can adjust your code: > > upload_url = f"{DIFY_API_BASE}/datasets/{DIFY_DATASET_ID}/documents" > files_payload = { > 'file': (file_name, file_content, 'text/html'), > 'data': (None, json.dumps({ > "doc_metadata": metadata, > "process_rule": "your_process_rule_here" # Replace with your actual process rule > }), 'application/json') > } > response = requests.post(upload_url, headers=DIFY_HEADERS, files=files_payload) > Make sure your Dify backend is running and accessible at the expected URL and port. If you still get a 404 at the correct endpoint, double-check your Docker setup and that the API service is healthy. > > Let me know if you need more details on the process_rule or encounter a different error! > > _To reply, just mention [@dosu](https://go.dosu.dev/dosubot)._ > > How did I do? [Good](https://app.dosu.dev/response-feedback/7616054f-b1ec-4b6a-852b-d5b26939813c?feedback_type=great_response) | [Irrelevant](https://app.dosu.dev/response-feedback/7616054f-b1ec-4b6a-852b-d5b26939813c?feedback_type=irrelevant_answer) | [Incorrect](https://app.dosu.dev/response-feedback/7616054f-b1ec-4b6a-852b-d5b26939813c?feedback_type=incorrect_sources) | [Verbose](https://app.dosu.dev/response-feedback/7616054f-b1ec-4b6a-852b-d5b26939813c?feedback_type=too_verbose) | [Hallucination](https://app.dosu.dev/response-feedback/7616054f-b1ec-4b6a-852b-d5b26939813c?feedback_type=hallucination) | [Report 🐛](https://app.dosu.dev/response-feedback/7616054f-b1ec-4b6a-852b-d5b26939813c?feedback_type=bug_report) | [Other](https://app.dosu.dev/response-feedback/7616054f-b1ec-4b6a-852b-d5b26939813c?feedback_type=other)  [![Join Discord](https://camo.githubusercontent.com/d562c2df5e4567ea41d37f1d91a4da0839d8341037045b103b2da268998de17f/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6a6f696e2d3538363546323f6c6f676f3d646973636f7264266c6f676f436f6c6f723d7768697465266c6162656c3d)](https://go.dosu.dev/discord-bot) [![Share on X](https://camo.githubusercontent.com/4649f705e2396d6f328dbc8e800a0509c9c17b3b20253b36706c393df124b93d/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f582d73686172652d626c61636b)](https://twitter.com/intent/tweet?text=%40dosu_ai%20helped%20me%20solve%20this%20issue!&url=https%3A//github.com/langgenius/dify/issues/21388) > The 404 error happens because /v1/datasets/{dataset_id}/documents/upload is not a valid endpoint in Dify 1.4.3. The correct endpoint for uploading documents is /v1/datasets/{dataset_id}/documents using a POST request with multipart/form-data. > > Recent changes also require a process_rule parameter in your upload payload, or the upload may fail (though usually not with a 404) [source](https://github.com/langgenius/dify/pull/20599). > > Here's how you can adjust your code: > > upload_url = f"{DIFY_API_BASE}/datasets/{DIFY_DATASET_ID}/documents" > files_payload = { > 'file': (file_name, file_content, 'text/html'), > 'data': (None, json.dumps({ > "doc_metadata": metadata, > "process_rule": "your_process_rule_here" # Replace with your actual process rule > }), 'application/json') > } > response = requests.post(upload_url, headers=DIFY_HEADERS, files=files_payload) > Make sure your Dify backend is running and accessible at the expected URL and port. If you still get a 404 at the correct endpoint, double-check your Docker setup and that the API service is healthy. > > Let me know if you need more details on the process_rule or encounter a different error! > > _To reply, just mention [@dosu](https://go.dosu.dev/dosubot)._ > > How did I do? [Good](https://app.dosu.dev/response-feedback/7616054f-b1ec-4b6a-852b-d5b26939813c?feedback_type=great_response) | [Irrelevant](https://app.dosu.dev/response-feedback/7616054f-b1ec-4b6a-852b-d5b26939813c?feedback_type=irrelevant_answer) | [Incorrect](https://app.dosu.dev/response-feedback/7616054f-b1ec-4b6a-852b-d5b26939813c?feedback_type=incorrect_sources) | [Verbose](https://app.dosu.dev/response-feedback/7616054f-b1ec-4b6a-852b-d5b26939813c?feedback_type=too_verbose) | [Hallucination](https://app.dosu.dev/response-feedback/7616054f-b1ec-4b6a-852b-d5b26939813c?feedback_type=hallucination) | [Report 🐛](https://app.dosu.dev/response-feedback/7616054f-b1ec-4b6a-852b-d5b26939813c?feedback_type=bug_report) | [Other](https://app.dosu.dev/response-feedback/7616054f-b1ec-4b6a-852b-d5b26939813c?feedback_type=other)  [![Join Discord](https://camo.githubusercontent.com/d562c2df5e4567ea41d37f1d91a4da0839d8341037045b103b2da268998de17f/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6a6f696e2d3538363546323f6c6f676f3d646973636f7264266c6f676f436f6c6f723d7768697465266c6162656c3d)](https://go.dosu.dev/discord-bot) [![Share on X](https://camo.githubusercontent.com/4649f705e2396d6f328dbc8e800a0509c9c17b3b20253b36706c393df124b93d/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f582d73686172652d626c61636b)](https://twitter.com/intent/tweet?text=%40dosu_ai%20helped%20me%20solve%20this%20issue!&url=https%3A//github.com/langgenius/dify/issues/21388) Thanks. You are right! But now I encountered a new issue: Attempting to upload: .html with metadata: Received HTTP Status Code: 405 Received Raw Response Text (first 500 chars): '{"code": "method_not_allowed", "message": "The method is not allowed for the requested URL.", "status": 405} ...' FAILURE: Uploaded .html with non-success status code: 405 @dosu
Author
Owner

@crazywoola commented on GitHub (Jun 23, 2025):

Check the environment settings FILES_URL, please read the docs.

@crazywoola commented on GitHub (Jun 23, 2025): Check the environment settings `FILES_URL`, please read the docs.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langgenius/dify#14820