当我尝试执行下面的代码时,它只是导致无效的内容类型(具有错误编号612(。
我正在尝试从静态列表中删除铅ID。我可以添加潜在客户ID或获得静态列表符号。
我拨打的帖子和接听电话很好,尽管我打来的帖子呼叫似乎需要url字符串上的数据(如$ {endpointurl}/rest/rest/v1/lists/lists/{listId}/ledss/leads。
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调用之前添加此行,它甚至在登录DeleteSync调用之前就会给我一个不同的错误。
client.DefaultRequestHeaders.Add("Content-Type", "application/json");
错误是"滥用标题名称。请确保请求标题与httprequestmessage一起使用,带有httpresponsemessage的响应标头以及带有httpcontent对象的内容标题。"
尝试在您的代码中使用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);
解决方案被证明是几个建议的组合。
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);
这对我有用。