Do you return a Resource URL?

Loading last updated info...

When the Web API creates a resource, it should include the URI of the new resource in the Location header of the response.

public Product PostProduct(Product item)
{
item = repository.Add(item);
return item;
}

❌ Figure: Figure: Bad example – The response does not contain a reference to the location of the new resource

public HttpResponseMessage PostProduct(Product item)
{
item = repository.Add(item);
var response = Request.CreateResponse(HttpStatusCode.Created, item);
string uri = Url.Link("DefaultApi", new { id = item.Id });
response.Headers.Location = new Uri(uri);
return response;
}

✅ Figure: Figure: Good example – The response message contains a link in the header to the created resource (plus the “Created” status code is returned)

Categories

Authors

Need help?

SSW Consulting has over 30 years of experience developing awesome software solutions.

We open source.Loving SSW Rules? Star us on GitHub. Star