将对象添加到httpclient put

时间:2019-01-29 15:46:18

标签: c# rest dotnet-httpclient put

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

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软件包。

0 个答案:

没有答案