C#donds';不起作用,同样的叫声在招摇中起作用

C#donds';不起作用,同样的叫声在招摇中起作用,c#,rest,C#,Rest,我想使用一个有招摇过市的外部API。我大摇大摆地称此url为: PATCH /rest/inventory/item/{id} 参数为:X-Auth-Token、id和patchOperations,如下所示: [ { "op": "replace", "path": "price", "value": 6.2 } ] 当我用这些参数调用这个方法时,它是有效的。

我想使用一个有招摇过市的外部API。我大摇大摆地称此url为:

PATCH /rest/inventory/item/{id}
参数为:X-Auth-Token、id和patchOperations,如下所示:

[
  {
    "op": "replace",
    "path": "price",
    "value": 6.2
  }
]
当我用这些参数调用这个方法时,它是有效的。我得到了成功代码200,之后当我调用get方法时,我看到该商品的价格已更新为6.2

现在我想用C#来做这个。我已经成功地从同一个API调用了一些GET方法。这是我的补丁方法代码:

var model = new Dictionary<string, object>
             {
                {"op", "replace"},
                {"path", "price"},
                {"value", 6}
             };

var blabla = await _httpProvider.PatchAsync($"https://{url}/server/rest/inventory/item/{id}", model, null, null, null, connection.Request.HeaderParameters);

public async Task<HttpResponseModel> PatchAsync<T>(string uri, T data, HttpClientHandler httpClientHandler = null, TimeSpan? timeout = null, string contentTypes = null, Dictionary<string, string> headerParameters = null)
    {
        using (var client = CreateHttpClient(httpClientHandler, timeout, contentTypes, headerParameters))
        {
            var requestContent = new StringContent(JsonConvert.SerializeObject(data));
            var response = await client.PatchAsync(new Uri(uri), requestContent);
            var result = new HttpResponseModel
            {
                Success = response.IsSuccessStatusCode,
                ResponseContent = await response.Content.ReadAsStringAsync(),
                ResponseTime = sw.Elapsed
            };

            return result;
        }
    }
var模型=新字典
{
{“op”,“replace”},
{“路径”,“价格”},
{“值”,6}
};
var blabla=await_httpProvider.PatchAsync($“https://{url}/server/rest/inventory/item/{id}”,model,null,null,null,connection.Request.HeaderParameters);
公共异步任务PatchAsync(字符串uri,T数据,HttpClientHandler HttpClientHandler=null,TimeSpan?timeout=null,字符串contentTypes=null,字典头参数=null)
{
使用(var client=CreateHttpClient(httpClientHandler、超时、内容类型、headerParameters))
{
var requestContent=newstringcontent(JsonConvert.SerializeObject(数据));
var response=await client.PatchAsync(新Uri(Uri)、requestContent);
var结果=新的HttpResponseModel
{
成功=响应。IsSuccessStatusCode,
ResponseContent=wait response.Content.ReadAsStringAsync(),
ResponseTime=sw.passed
};
返回结果;
}
}

我的错在哪里?我收到错误状态代码:500,原因短语:“服务器错误”,版本:1.1,内容:System.Net.Http.StreamContent

错误在于您没有粘贴相同的内容,无论如何也不是这样

您的
PATCH
示例是一个包含3个属性的对象的数组,在您的示例中,数组中只有1个元素,但它仍然是一个数组。您的C#被序列化为单个对象

这很微妙,但您发送的JSON实际上是:

{
“op”:“替换”,
“路径”:“价格”,
“价值”:6
}
因此,您需要在数组中发送字典或其他对象:

var model = new List<object> {
    {
        new Dictionary<string, object>
        {
            { "op", "replace"},
            {"path", "price"},
            {"value", 6}
        }
    };

错误在于你没有粘贴相同的内容,无论如何也不是

您的
PATCH
示例是一个包含3个属性的对象的数组,在您的示例中,数组中只有1个元素,但它仍然是一个数组。您的C#被序列化为单个对象

这很微妙,但您发送的JSON实际上是:

{
“op”:“替换”,
“路径”:“价格”,
“价值”:6
}
因此,您需要在数组中发送字典或其他对象:

var model = new List<object> {
    {
        new Dictionary<string, object>
        {
            { "op", "replace"},
            {"path", "price"},
            {"value", 6}
        }
    };
斯威格是用C写的,所以这不是一个C问题,这是我C代码中的一个问题。斯威格是用C写的,所以这不是一个C问题,这是我C代码中的一个问题。