我正在像这样进行 HTTP 调用:
[HttpGet]
public HttpResponseMessage updateRegistrant(string token, string registrantId, string firstname, string lastname, string postalCode, string phoneNumber, string city, string email)
{
using (HttpClient httpClient = new HttpClient())
{
httpClient.BaseAddress = new Uri("https://api.example.com/v1/registrants/" + registrantId + "/");
httpClient.DefaultRequestHeaders.Accept.Clear();
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Put, "person/contact-information");
request.Content = new StringContent("{\"firstName\":\"" + firstname + "\", \"lastName\":\"" + lastname + "\", \"phones\":[{\"phone\":\"" + phoneNumber + "\", \"type\":\"Home\", \"primary\":true}], \"emails\":[{\"email\":\"" + email + "\", \"type\":\"Personal\", \"primary\":true}], \"addresses\":[{\"city\":\"" + city + "\", \"zipCode\":\"" + postalCode + "\"}]}", Encoding.UTF8, "application/json");
//request.Content = new StringContent("{\"firstName\":\"" + firstname + "\", \"lastName\":\"" + lastname + "\"}", Encoding.UTF8, "application/json");
HttpResponseMessage response = httpClient.SendAsync(request).Result;
return response;
}
}
现在,当我运行此方法时,我得到 409 错误调用,但是如果我注释掉第一个 request.Content 并取消注释第二个 request.Content 它可以工作,我得到的响应代码为 200。
我会假设这些导致 409 错误:
\"phones\":[{\"phone\":\"" + phoneNumber + "\", \"type\":\"Home\", \"primary\":true}]
但是为什么以及如何解决这个问题?