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

Azure Table Storage REST API InsertOrMerge返回ResourceNotFound问题排查

修复方案

你当前遇到的ResourceNotFound报错是因为InsertOrMerge操作存在几项缺失的必填配置,具体修复点如下:

  • 新增必填请求头If-None-Match: *
    普通MERGE操作仅能更新已存在的实体,实体不存在时就会返回404错误。InsertOrMerge属于Upsert操作,必须添加If-None-Match: *请求头,服务端收到这个头后会判断:实体不存在就执行插入,存在就执行合并,不会抛出资源不存在错误。同理InsertOrUpdate操作也需要添加这个头才能实现不存在即插入的逻辑。
  • 修正x-ms-date的格式
    Azure REST API要求x-ms-date必须符合RFC1123格式,你当前使用的默认toString()输出的格式不符合校验规则,会导致请求被拒绝。你可以借助intl包格式化日期:
    import 'package:intl/intl.dart';
    // 格式化RFC1123格式的UTC时间
    final rfc1123Format = DateFormat('EEE, dd MMM yyyy HH:mm:ss ' 'GMT');
    String formattedDate = rfc1123Format.format(DateTime.now().toUtc());
    
  • 修正代码语法和参数问题
    1. Dart http库的Request类请求头属性是复数headers,你代码中写的request.header是错误写法,会导致所有头配置失效。
    2. 请求体不能直接赋值Map对象,需要先序列化为JSON字符串:
    request.body = jsonEncode({
      "PartitionKey": "new key",
      "RowKey": "mykey"
    });
    
    1. 检查URL中$id部分的格式必须为PartitionKey='你的分区键', RowKey='你的行键',且和请求体中的PartitionKey、RowKey值完全一致,否则也会触发资源不存在错误。

修复后的参考代码如下:

import 'dart:convert';
import 'package:intl/intl.dart';
import 'package:http/http.dart' as http;

String _urlString = '$endpoint($id)?$sas';
var url = Uri.parse(_urlString);
var request = http.Request('MERGE', url);

// 格式化符合要求的UTC时间
final rfc1123Format = DateFormat('EEE, dd MMM yyyy HH:mm:ss ' 'GMT');
String formattedDate = rfc1123Format.format(DateTime.now().toUtc());

request.headers = {
  "Accept": "application/json;odata=nometadata",
  "x-ms-date": formattedDate,
  "x-ms-version": "2020-10-02",
  "Content-Type": "application/json; charset=utf-8",
  "If-None-Match": "*" // InsertOrMerge必填请求头
};

request.body = jsonEncode({
  "PartitionKey": "new key",
  "RowKey": "mykey"
});

await request.send();

内容的提问来源于stack exchange,提问作者Mohamed RT

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.24 00:06:02