You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在RAD Studio 12.3中配置Claude API实现智能代码洞察遇NotFound错误

Claude API在Embarcadero 12.3中报「Not Found」的排查方案

核心排查与修复步骤

  • API端点校验
    确认使用的Claude API端点是否为Anthropic官方当前有效版本,主流v2接口端点为https://api.anthropic.com/v1/messages,误用旧版v1的completions类接口会直接返回404。检查Embarcadero代码中THTTPClient的请求目标URL是否完全匹配。
  • 密钥与请求头配置
    确保API密钥是Anthropic官方生成的有效密钥,无空格或特殊字符冗余。请求头必须包含x-api-key和anthropic-version(如2023-06-01)两个必填字段,在THTTPClient.CustomHeaders中确认这两个字段的拼写和值是否正确。
  • 请求体格式合规性
    Claude API要求请求体为标准JSON结构,必填字段包括model(如claude-3-sonnet-20240229)、max_tokens、messages数组。使用TJSONObject构造请求时,需保证messages数组中每个元素包含role(user/assistant)和content字段,数据类型完全匹配要求。
  • 网络与协议适配
    确认Embarcadero 12.3的THTTPClient启用了TLS 1.2及以上版本协议,可通过设置HTTPClient.Settings.SSLProtocols := [TSSLProtocol.tls12, TSSLProtocol.tls13]强制适配。同时排查本地防火墙、代理是否拦截了对api.anthropic.com的请求。

修正后的代码示例

var
  HTTPClient: THTTPClient;
  RequestBody: TJSONObject;
  MsgArray: TJSONArray;
  Response: IHTTPResponse;
begin
  HTTPClient := THTTPClient.Create;
  try
    // 配置TLS协议与请求头
    HTTPClient.Settings.SSLProtocols := [TSSLProtocol.tls12, TSSLProtocol.tls13];
    HTTPClient.CustomHeaders['x-api-key'] := '你的Claude API密钥';
    HTTPClient.CustomHeaders['anthropic-version'] := '2023-06-01';

    // 构造合规请求体
    RequestBody := TJSONObject.Create;
    MsgArray := TJSONArray.Create;
    try
      RequestBody.AddPair('model', 'claude-3-sonnet-20240229');
      RequestBody.AddPair('max_tokens', TJSONNumber.Create(1024));
      
      MsgArray.AddElement(TJSONObject.Create(
        TJSONPair.Create('role', 'user'),
        TJSONPair.Create('content', '请提供技术支持')
      ));
      RequestBody.AddPair('messages', MsgArray);

      // 发送请求并处理响应
      Response := HTTPClient.Post('https://api.anthropic.com/v1/messages', RequestBody.ToString, TEncoding.UTF8);
      if Response.StatusCode = 200 then
        ShowMessage(Response.ContentAsString)
      else
        ShowMessage('错误: ' + Response.StatusText + ' (' + IntToStr(Response.StatusCode) + ')');
    finally
      MsgArray.Free;
      RequestBody.Free;
    end;
  finally
    HTTPClient.Free;
  end;
end;

内容的提问来源于stack exchange,提问作者Stefan de Beer

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.06 06:12:02