How to create a self hosted API for development and testing
jeremymorgan.com
How to create a self hosted API for development and testing
1–10 of 12 posts
Re: How to create a self hosted API for development and testing
#2Re: How to create a self hosted API for development and testing
#3Re: How to create a self hosted API for development and testing
#4 netsh http add urlacl url=http://+:8080/ user=DOMAIN\username
You don't need attributes based routing in this example. Convention based routing is built into WebAPI.For the Delete method you can return an HttpStatusCode instead of throwing an exception.
public IHttpActionResult Delete(int Id)
{
var result = (from b in ourbooks
where b.Id == Id
select b).FirstOrDefault();
ourbooks.Remove(result);
return StatusCode(HttpStatusCode.Accepted);
}
Also check out the OWIN self host tutorial [2].[1] https://github.com/NancyFx/Nancy/wiki/Hosting-nancy-with-owi...
[2] http://www.asp.net/web-api/overview/hosting-aspnet-web-api/u...
Re: How to create a self hosted API for development and testing
#5Does anyone know of a similar guide for Ruby? I'm an ASP.net developer and I'm trying to get some insight into other (perhaps quicker/easier) ways of doing exactly what this guide describes.
Tho, http://requestb.in/ is a superb tool as well as https://www.runscope.com if you need more power.
p.s. this one too : http://httpbin.org/
Re: How to create a self hosted API for development and testing
#6Re: How to create a self hosted API for development and testing
#7Apiary is also a good option ( http://apiary.io ).
Re: How to create a self hosted API for development and testing
#8Here's a quick take. I'm sure it can be cleaned up further. I've also taken the liberty of fixing the delete before post id bug.
express = require 'express'
bodyParser = require 'body-parser'
server = express()
server.use bodyParser()
books = [
{ id: 1, title: "Microsoft Visual C# 2012", author: "John Sharp" }
{ id: 2, title: "C# 5.0 in a nutshell", author: "Joseph Albahari" }
{ id: 3, title: "C# in Depth, 3rd Edition", author: "Jon Skeet" }
{ id: 4, title: "Pro ASP.NET MVC 5", author: "Adam Freeman" }
]
findBook = (req, res, cb) ->
book = books.filter (b) -> b.id is +req.params.id
if book.length
cb book[0]
else
res.statusCode = 404
res.send "No book with ID = #{req.params.id}"
server.get "/api/books", (req, res) ->
res.send books
server.get "/api/books/:id", (req, res) ->
findBook req, res, (book) ->
res.send book
server.post "/api/books", (req, res) ->
book = req.body
book.id = 1 + Math.max.apply Math, books.map (b) -> b.id
books = books.concat [book]
res.send 200
server.put "/api/books/:id", (req, res) ->
findBook req, res, (book) ->
updates = req.body
['title', 'author'].forEach (f) -> book[f] = updates[f]
res.send 200
server.delete "/api/books/:id", (req, res) ->
findBook req, res, (book) ->
books = books.filter (b) -> b.id isnt book.id
res.send 200
server.listen 3000
console.log "server listening on localhost:3000"Re: How to create a self hosted API for development and testing
#9 var resp = new HttpResponseMessage(HttpStatusCode.Created);
throw new HttpResponseException(resp);Re: How to create a self hosted API for development and testing
#10Apiary is also a good option ( http://apiary.io ).
i've been using apiary this week and it's been absolutely fantastic so far. Markdown goes in, API docs + mock live API comes out, even syncs with github.