Мой код стороны API выглядит следующим образом:
[HttpPost]
[Route("api/Login")]
public HttpResponseMessage ValidateLogin(UserModel user)
{
IEnumerable<string> customJsonInputString;
if (!Request.Headers.TryGetValues("Content-Type", out customJsonInputString))
return new HttpResponseMessage(HttpStatusCode.BadRequest);
var customJsonInputArray = customJsonInputString.ToArray();
var ProductsRequest =
Newtonsoft.Json.JsonConvert.DeserializeObject<UserModel>(customJsonInputArray[0]);
var result = _service.Fetch(
new UserModel
{
Username = user.Username,
Password = user.Password.GenerateHash()
}
);
return Request.CreateResponse(HttpStatusCode.OK, result);
}
Я пытаюсь вызвать его из отдельного проекта, который находится в том же решении:
[HttpPost]
public async Task<ActionResult> Login(UserLoginModel user)
{
UserModel data = new UserModel
{
Username = user.Username,
Password = user.Password
};
using (var client = new HttpClient())
{
var myContent = JsonConvert.SerializeObject(data);
var buffer = Encoding.UTF8.GetBytes(myContent);
var byteContent = new ByteArrayContent(buffer);
byteContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
var endpoint = "http://localhost:55042/api/Login";
var response = await client.PostAsync(endpoint, byteContent);
throw new NotImplementedException();
}
}
Проблема IЯ думаю, что это имя первого параметра Request.Headers.TryGetValues("Content-Type", out customJsonInputString)
, я искал в Интернете, но не нашел правильного описания / объяснения, каким должно быть это имя параметра (ну, я понял, что это имя заголовка, но я попыталсячтобы найти его также с помощью «ContentType» и результат тот же: «400 неверных запросов»), поэтому мои вопросы:
- Что я делаю не так?
- Это неправильночто я предположил, что имя заголовка будет «ContentType» или «Content-Type»?