Nikita
Nikita

Reputation: 51

IHTTPActionResult Ok<T>(T Content) gives HTTP/1.1 406 Not Acceptable

When I write return OK() then it gives me 201 HTTPStatusCde. However when i am trying to get a response object in return, it gives me 406 error. Here var response is my custom schema in which i want a response

public IHttpActionResult Post(Order order)
    {
        if (!ModelState.IsValid)
        {
            return BadRequest(ModelState);
        }
        try
        {
            var response = this._orderRepository.AddReturnOrder(order);
            if (response != null)
                return Ok(response);

            return NotFound();
        }
        catch(Exception ex)
        {
            return InternalServerError(ex);
        }       

    }

Upvotes: 1

Views: 1446

Answers (2)

QianLi
QianLi

Reputation: 1098

First, what exact do you want to return by the "var response"? Please be noted that for OData protocol, the POST request return either "201 Created" or "204 no content". In case the response code is 201, the response body MUST contain the resource created. So the standard way to do a POST in OData Web API is like :

    // POST odata/Foos
    public IHttpActionResult Post(Foo foo)
    {
        if(!ModelState.IsValid)
        {
            return BadRequest(ModelState);
        }

        FakeData.Instance.Foos.Add(foo);
        return Created(foo);
    }

Upvotes: 0

Joona Romppanen
Joona Romppanen

Reputation: 66

Make sure your returning object is serializable to JSON or XML. Otherwise the controller method is unable to serve content that would be acceptable according to the Accept headers as indicated by the HTTP status code definition:

406 Not Acceptable The requested resource is only capable of generating content not acceptable according to the Accept headers sent in the request.

Since I was unable to comment for additional information due to low rep, can you give us the object model you're trying to return in the response?

Upvotes: 1

Related Questions