0

Когда я POST этот запрос http://localhost:61679/api/Login:415 при передаче параметра строки в Web Api сообщение метода

Headers => Content-Type: text/plain 
Body =>  grant_type=password&username=doug&password=12345 

я 415 Unsupported Media Type:

HTTP/1.1 415 Unsupported Media Type 
Cache-Control: no-cache 
Pragma: no-cache 
Content-Type: application/json; charset=utf-8 
Expires: -1 
Server: Microsoft-IIS/10.0 
X-AspNet-Version: 4.0.30319 
X-SourceFiles: =?UTF-8?B?QzpcZGV2ZWxvcG1lbnRcYml0Y29pblxwYXltZW50cy1zZXJ2aWNlLW5vLWNoZWNrc3VtLW5vLXVzZXJcUGF5bWVudHNTZXJ2aWNlXGFwaVxBdXRoZW50aWNhdGU=?= 
X-Powered-By: ASP.NET 
Date: Mon, 28 Dec 2015 16:58:36 GMT 
Content-Length: 900 

{"Message":"The request entity's media type 'text/plain' is not supported for this resource.","ExceptionMessage":"No MediaTypeFormatter is available to read an object of type 'String' from content with media type 'text/plain'.","ExceptionType":"System.Net.Http.UnsupportedMediaTypeException","StackTrace":" at System.Net.Http.HttpContentExtensions.ReadAsAsync[T](HttpContent content, Type type, IEnumerable`1 formatters, IFormatterLogger formatterLogger, CancellationToken cancellationToken)\r\n at System.Net.Http.HttpContentExtensions.ReadAsAsync(HttpContent content, Type type, IEnumerable`1 formatters, IFormatterLogger formatterLogger, CancellationToken cancellationToken)\r\n at System.Web.Http.ModelBinding.FormatterParameterBinding.ReadContentAsync(HttpRequestMessage request, Type type, IEnumerable`1 formatters, IFormatterLogger formatterLogger, CancellationToken cancellationToken)"} 

Контроллер:

// POST api/Login 
    public HttpResponseMessage Post([FromBody]string loginDetails) 
    { 
     // process loginDetails here 
    } 
+1

Любопытно - зачем вы принимаете строку в контроллере, но похоже, что вы передаете ex object? –

+0

Строка в теле должна быть указана. Content-Type должен быть application/json. http://stackoverflow.com/a/41114909/4854931 – Alex78191

ответ

3

You необходимо установить свой Content-Type: application/json; кодировка = UTF-8 вместо текста/равнины

должен создать модель представления

public class LoginDetails{ 
public string grant_type{get;set;} 
public string username{get;set;} 
public string password{get;set;} 
} 

вашего результат действия будет как

public HttpResponseMessage Post(LoginDetails loginDetails) 

времени запроса ваше тело будет как

{ 
    "grant_type":"...", 
    "username":".....", 
    "password":"....." 
} 
Смежные вопросы