-1

When I try to do the code below, it just results in Invalid Content Type (with error number 612).

I'm trying to delete a lead id from a static list. I can add lead ids or get the static list leads fine.

The post and get calls I make are working fine, although the post calls I make seem to require the data right on the url string (as in $"{endpointURL}/rest/v1/lists/{listID}/leads.json?id={leadID}"; If I include the id as a json object, it fails too. This might be a clue to what I'm doing wrong with the delete call.

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);

The response here always results in Invalid Content Type.

If I add this line before I do the deleteasync call, it gives me a different error before it even hits the deleteAsync call.

client.DefaultRequestHeaders.Add("Content-Type", "application/json");

Error is "Misused header name. Make sure request headers are used with HttpRequestMessage, response headers with HttpResponseMessage, and content headers with HttpContent objects."

Servy
  • 202,030
  • 26
  • 332
  • 449
Jim
  • 117
  • 1
  • 14
  • Hope you have checked this link https://stackoverflow.com/questions/28181325/why-invalid-content-type . Check stephenking answer. for ContentType and Accept – Mdyahiya Jul 01 '19 at 22:06
  • Yeah, I checked that out. That was what led me to add adding the content-type header that causes an exception before the deleteAsync call itself. Also, the link to the marketo examples is no longer active. – Jim Jul 01 '19 at 22:10
  • 1
    Check out [this](https://stackoverflow.com/questions/10679214/how-do-you-set-the-content-type-header-for-an-httpclient-request) which addresses the error you're getting when you add the header. – Scott Hannen Jul 01 '19 at 22:41
  • @Scott Thanks. That did the trick. Much appreciated. – Jim Jul 01 '19 at 23:17
  • 1
    If you want to answer the question then *post an answer*, don't edit the question. – Servy Jul 01 '19 at 23:20
  • Possible duplicate of [How do you set the Content-Type header for an HttpClient request?](https://stackoverflow.com/questions/10679214/how-do-you-set-the-content-type-header-for-an-httpclient-request) – Mark Schultheiss Jul 01 '19 at 23:20
  • No. It's not a duplicate. I did try that solution but that solution does not allow for DeleteAsync calls. It doesn't work for my issue. I actually stated that to someone else who already asked, but you didn't actually read it obviously. – Jim Jul 01 '19 at 23:28
  • @servy actually I wasn't able to add answers before, afaik. I didn't have enough reputation. Did that change? – Jim Jul 03 '19 at 22:22
  • @Jim You do not need any reputation to post answers. Any account can always post answers, unless it's been suspended, which you were not. – Servy Jul 03 '19 at 22:27

2 Answers2

0

Try using HttpRequestMessage in your code like this

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);
jPhizzle
  • 487
  • 1
  • 5
  • 16
  • Still doesn't work. Actually with that change, I get the error "Access Token Not Specified". If I comment the request.Headers.Add("Bearer", _access_token); call and add the original client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", _access_token), I get the same Invalid Content Type error. – Jim Jul 01 '19 at 22:38
  • i think i have an idea...give me a second... hehe – jPhizzle Jul 01 '19 at 22:46
  • 1
    @Jim ok I've updated my answer. if possible, can you tell me the actual api endpoint you're trying to get to? i really don't think `leads.json?id=` should be part of the route.. so i removed it from my answer. and also created a dictionary key value pair object to pass in as the Content... hopefully this will resolve the Content Type error – jPhizzle Jul 01 '19 at 23:08
  • 1
    Thanks J, it's solved now. I just needed to use your suggestions from before along with adding "request.Content = new StringContent(string.Empty, Encoding.UTF8, "application/json");" Thanks for your help. Much appreciated! – Jim Jul 01 '19 at 23:19
  • 1
    NICE! Thank you for letting me be of service! – jPhizzle Jul 01 '19 at 23:23
0

The solution turned out to be a combination of a couple of suggestions.

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);

This worked for me.

Jim
  • 117
  • 1
  • 14