public async static Task<RootUserData> getUSerLoggedIn(string userName, string password)
{
RootUserData rootUserData = new RootUserData();
var url = URlConstants.LOGIN_URL;
using (HttpClient httpClient = new HttpClient())
{
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes($"{userName}:{password}");
httpClient.DefaultRequestHeaders.Add("content-type", "application/json");
httpClient.DefaultRequestHeaders.Add("cache-control", "no-cache");
} ;
}
我正在使用以上代码进行一次服务呼叫。我必须像邮递员图片中所示将userEmailAddress传递到正文中。能帮我吗?
答案 0 :(得分:1)
否...纯文本格式
将内容MIME类型设置为“文本/纯文本”:
httpClient.DefaultRequestHeaders.Add("content-type", "text/plain");
并发布一个字符串:
var response = await httpClient.PostAsync(url, new StringContent(userName));
答案 1 :(得分:0)
不应该这样添加Content-Type,在我的情况下,它不起作用,而是给出了错误的响应
像这样传递内容类型-
httpRequest.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/x-www-form-urlencoded");