Skip to content

DeleteDocument API 返回 204 导致 JSON 反序列化失败 #14

@fengb3

Description

@fengb3

Bug 描述

调用 KnowledgeBaseClient.DeleteDocument() 时,Dify API 返回 HTTP 200,body 为纯数字 204,导致 System.Text.Json 无法将其反序列化为 Delete.ResponseBody(期望 JSON 对象 {"result": "..."}),抛出 JsonException

复现步骤

  1. 调用 client.DeleteDocument(datasetId, documentId) 删除一个已存在的文档
  2. Dify API 返回:HTTP 200,body 为 204(纯数字,非 JSON 对象)

实际行为

HTTP/1.1 200 OK
Content-Type: application/json
Content-Length: 4

204

抛出异常:

System.Text.Json.JsonException: The JSON value could not be converted to 
DifySharp.KnowledgeBase.Document.Delete+ResponseBody. 
Path: $ | LineNumber: 0 | BytePositionInLine: 3.

期望行为

DeleteDocument 应能正确处理 Dify 返回的 204 响应,不抛出异常。

环境信息

  • DifySharp: 0.0.3-alpha5
  • Dify: 1.9.2 (自部署)
  • .NET: 8.0

临时解决方案

在调用端 catch HttpRequestException(内含 JsonException)来绕过此问题:

try
{
    await client.DeleteDocument(datasetId, documentId);
}
catch (HttpRequestException ex) when (ex.InnerException is System.Text.Json.JsonException)
{
    // Dify returns plain "204" on successful delete, which fails JSON deserialization
}

建议修复方向

IDocumentApiDeleteDocument 接口上,考虑让返回类型兼容非 JSON 响应,或在反序列化前对响应内容做预处理(如判断为纯数字时跳过反序列化)。

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions