Vote count:
0
I created a .NET 4.5 Web project using Visual Studio 2012 to test out using OData following this tutorial. However, whenever I send the following request from Fiddler:
PUT http://localhost:14116/odata/Products(5) HTTP/1.1
Host: localhost:14116
Accept: application/json
Content-Type: application/json
Content-Length: 67
{ "Name" : "Apple //e", "Price" : "1298", "Category" : "Computer" }
I get the following response:
HTTP/1.1 400 Bad Request
Cache-Control: no-cache
Pragma: no-cache
Expires: -1
Server: Microsoft-IIS/8.0
X-AspNet-Version: 4.0.30319
X-SourceFiles: =?UTF-8?B?YzpcdXNlcnNcZGxhdmVsbGVcZG9jdW1lbnRzXHZpc3VhbCBzdHVkaW8gMjAxMlxQcm9qZWN0c1xP RGF0YVRlc3QzXE9EYXRhVGVzdDNcb2RhdGFcUHJvZHVjdHMoNSk=?=
X-Powered-By: ASP.NET
Date: Wed, 01 Apr 2015 14:37:41 GMT
Content-Length: 0
What am I missing? (Stack overflow is removing two empty lines in the HTTP response.)
This request works fine:
GET http://localhost:14116/odata/Products?$filter=Price+lt+500 HTTP/1.1
Host: localhost:14116
Accept: application/json
Here's my controller:
public class ProductsController : ODataController
{
static List<Product> s_products;
static ProductsController()
{
s_products = new List<Product>();
s_products.Add(
new Product
{
Id = 1
, Name = "Nexus 7"
, Price = 179
, Category = "Tablet"
}
);
}
// GET odata/Products
[Queryable]
public IQueryable<Product> GetProducts()
{
return s_products.AsQueryable();
}
// GET odata/Products(5)
[Queryable]
public SingleResult<Product> GetProduct([FromODataUri] int key)
{
return SingleResult.Create(s_products.AsQueryable().Where(product => product.Id == key));
}
// PUT odata/Products(5)
public IHttpActionResult Put([FromODataUri] int key, Product product)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
if (key != product.Id)
{
return BadRequest();
}
var previous = s_products.SingleOrDefault(p => p.Id == key);
if (previous == null)
{
return NotFound();
}
previous.Category = product.Category;
previous.Name = product.Name;
previous.Price = product.Price;
return Updated(product);
}
// POST odata/Products
public IHttpActionResult Post(Product product)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
s_products.Add(product);
return Created(product);
}
// PATCH odata/Products(5)
[AcceptVerbs("PATCH", "MERGE")]
public IHttpActionResult Patch([FromODataUri] int key, Delta<Product> patch)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
Product product = s_products.SingleOrDefault(p => p.Id == key);
if (product == null)
{
return NotFound();
}
patch.Patch(product);
return Updated(product);
}
// DELETE odata/Products(5)
public IHttpActionResult Delete([FromODataUri] int key)
{
Product product = s_products.SingleOrDefault(p => p.Id == key);
if (product == null)
{
return NotFound();
}
s_products.Remove(product);
return StatusCode(HttpStatusCode.NoContent);
}
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
}
}
asked 35 secs ago
400 Bad Request for OData
Aucun commentaire:
Enregistrer un commentaire