首页 > 解决方案 > 将对象添加到 httpclient put

问题描述

我正在尝试向 API 发出 put 请求。到目前为止我的代码是 -

HttpClient client = new HttpClient()
        {
            BaseAddress = new Uri(baseAddress)
        };
        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
        client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Token", token);


var content = new FormUrlEncodedContent(new[]
        {
            new KeyValuePair<string, string>("id", id.ToString()),
            new KeyValuePair<string, string>("email", email),
            new KeyValuePair<string, string>("customer_group", group.ToString()),
            new KeyValuePair<string, string>("customer_permission", permission),
            new KeyValuePair<string, string>("prefered_timezone", timezone)
        });

        var response = await client.PutAsync(url, content);     

我遇到的问题是我也需要添加一个值字段。数据的样子——

values: [
0: {values: "+447500000000", field: 1082, user: 218218}
1: {values: "test", field: 1084, user: 218218}
2: {values: "name", field: 1085, user: 218218}]

我尝试为此创建一个对象,但由于使用 FormUrlEncodedContent 遇到问题。什么是替代方法?我也不能使用第三方 NuGet 包。

标签: c#restdotnet-httpclientput

解决方案


推荐阅读