我正在尝试使用HttpClient发布标准REST API。我定位的post方法需要两个键/值对,名称和大小。当我使用下面的代码执行帖子时,API会返回错误"缺少必需的键:大小。"但是,我已经明确地将它添加到FormUrlEncodedContent。
我想知道是否存在编码问题,或者我是否使用了错误的types of HttpContent。
public async Task<OHDrive> Create(OHDrive drive)
{
string json = "";
var values = new Dictionary<string, string>();
//name and size are required
if (string.IsNullOrEmpty(drive.Name) || drive.Size == 0)
throw new Exception("Name and Size are required for drive creation");
values["name"] = drive.Name;
values["size"] = drive.Size.ToString();
if (drive.UserID != null)
values["user"] = drive.UserID;
if (drive.Encryption != null)
values["encryption:cipher"] = drive.Encryption;
var content = new FormUrlEncodedContent(values);
using (HttpClient client = OHUtilities.CreateClient(userID, secretKey))
{
var url = urlBase + "create";
var response = await client.PostAsync(url, content);
json = await response.Content.ReadAsStringAsync();
}
return JsonConvert.DeserializeObject<OHDrive>(json);
}
更新
我尝试切换键/值对的顺序,如下所示:
values["size"] = drive.Size.ToString();
values["name"] = drive.Name;
然后我收到了相反的错误消息:&#34;缺少必需的密钥:名称。&#34;就像API因某种原因没有看到第二个参数。我在Fiddler检查了请求,一切看起来都是正确的。