How do you set the Content-Type header for an HttpClient request?

后端 未结 14 2435
暗喜
暗喜 2020-11-22 03:57

I\'m trying to set the Content-Type header of an HttpClient object as required by an API I am calling.

I tried setting the Content-Ty

14条回答
  •  无人共我
    2020-11-22 04:39

    You need to do it like this:

        HttpContent httpContent = new StringContent(@"{ the json string }");
        httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));                
        HttpResponseMessage message = client.PostAsync(@"{url}", httpContent).Result;
    

提交回复
热议问题