当我尝试执行以下代码时,它只会导致内容类型无效(错误号612)。
我正在尝试从静态列表中删除销售线索ID。我可以添加销售线索ID或获取静态列表销售线索。
我进行的发布和获取调用工作正常,尽管我进行的发布调用似乎要求直接在url字符串上输入数据(如$“ {endpointURL} / rest / v1 / lists / {listID} / leads .json?id = {leadID}“ ;;如果我将id包含为json对象,它也会失败。这可能是我在执行delete调用时出错的线索。
string url = $"{endpointURL}/rest/v1/lists/{listID}/leads.json?id={leadID}";
HttpClient client = new HttpClient();
client.BaseAddress = new Uri(url);
client.DefaultRequestHeaders.Authorization = new
AuthenticationHeaderValue("Bearer", _access_token);
HttpResponseMessage response = await client.DeleteAsync(url);
此处的响应始终会导致内容类型无效。
如果我在执行deleteasync调用之前添加了这一行,它甚至在到达deleteAsync调用之前都会给我一个不同的错误。
client.DefaultRequestHeaders.Add("Content-Type", "application/json");
错误是“报头名称滥用。请确保请求报头与HttpRequestMessage一起使用,响应报头与HttpResponseMessage一起使用,内容报头与HttpContent对象一起使用。”
答案 0 :(得分:0)
尝试像这样在代码中使用HttpRequestMessage
string url = $"{endpointURL}/rest/";
HttpClient client = new HttpClient
{
BaseAddress = new Uri(url)
};
//I'm assuming you have leadID as an int parameter in the method signature
Dictionary<string, int> jsonValues = new Dictionary<string, int>();
jsonValues.Add("id", leadID);
//create an instance of an HttpRequestMessage() and pass in the api end route and HttpMethod
//along with the headers
HttpRequestMessage request = new HttpRequestMessage
(HttpMethod.Delete, $"v1/lists/{listID}") //<--I had to remove the leads.json part of the route... instead I'm going to take a leap of faith and hit this end point with the HttpMethod Delete and pass in a Id key value pair and encode it as application/json
{
Content = new StringContent(new JavaScriptSerializer().Serialize(jsonValues), Encoding.UTF8, "application/json")
};
request.Headers.Add("Bearer", _access_token);
//since we've already told the request what type of httpmethod we're using
//(in this case: HttpDelete)
//we could just use SendAsync and pass in the request as the argument
HttpResponseMessage response = await client.SendAsync(request);
答案 1 :(得分:0)
解决方案原来是几个建议的组合。
HttpClient client = new HttpClient();
client.BaseAddress = new Uri(url);
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Delete, data);
// The key part was the line below
request.Content = new StringContent(string.Empty, Encoding.UTF8, "application/json");
if (!string.IsNullOrEmpty(_access_token))
{
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", _access_token);
}
HttpResponseMessage response = await client.SendAsync(request);
这对我有用。