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

后端 未结 14 2225
暗喜
暗喜 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:24

    Call AddWithoutValidation instead of Add (see this MSDN link).

    Alternatively, I'm guessing the API you are using really only requires this for POST or PUT requests (not ordinary GET requests). In that case, when you call HttpClient.PostAsync and pass in an HttpContent, set this on the Headers property of that HttpContent object.

提交回复
热议问题