c# - 将对象添加到 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 包。
解决方案
推荐阅读
- php - 如何从现有数组中生成良好的 JSON 输出?
- php - 将错误发送到哨兵,同时保留应用程序的 error_handler
- awk - 如何使用`:`和`=`在特定字符串之后插入行?
- assembly - 为什么存根指令会因总线错误而失败?
- reactjs - react-bootstrap 下拉菜单中的菜单对齐不起作用
- computer-vision - 最适合寻找方向的相机
- vue.js - 到达最后一条记录时如何禁用导航按钮?
- android - Android Amplify Auth 注销超时
- javascript - Angular:CORS 预检响应未成功
- vue.js - Vuetify:更改 v-application--wrap 的最小高度