APIs
api.route.delete()
Register a handler for HTTP DELETE requests to the route.
using Nitric.Sdk;
var route = Nitric.Api("main").Route("/customers");
route.Delete(context => {
// Construct a response for all incoming HTTP requests
var responseBody = new Dictionary<string, string>();
context.Res.Json(responseBody);
return context;
});
Nitric.Run();
Parameters
match required string
The path matcher to use for the route. Matchers accept path parameters in the form of a colon prefixed string. The string provided will be used as that path parameter's name when calling middleware and handlers. See create a route with path params
...middleware required Middleware<HttpContext>
or Func<HttpContext, HttpContext>
One or more middleware functions to use as the handler for HTTP requests. Handlers can be sync or async.
Examples
Register a handler for DELETE requests
using Nitric.Sdk;
var route = Nitric.Api("main").Route("/customers");
route.Delete(context => {
// Construct a response for all incoming HTTP requests
var responseBody = new Dictionary<string, string>();
context.Res.Json(responseBody);
return context;
});
Nitric.Run();
Chain functions as a single method handler
When multiple functions are provided they will be called as a chain. If one succeeds, it will move on to the next. This allows middleware to be composed into more complex handlers.
var route = Nitric.Api("main").Route("/customers/:userId");
route.Delete((context, next) => {
var user = context.Req.PathParams["userId"];
// Validate the user identity
if (user != "1234")
{
context.Res.Text($"User {user} is unauthorised");
context.Res.Status = 403;
// Return prematurely to end the middleware chain.
return context;
}
// Call next to continue the middleware chain.
return next(context);
}, (context, next) => {
var user = context.Req.PathParams["userId"];
context.Res.Text($"Deleting {user}");
return next(context);
}
);
Nitric.Run();
Access the request body
The DELETE request body is accessible from the ctx.req
object.
using Nitric.Sdk;
var api = Nitric.Api("main").Route("/customers");
route.Delete(context => {
var body = context.Req.FromJson<Dictionary<string, string>>();
// parse, validate and store the request payload...
});
Nitric.Run();