Skip to content

Commit c8f38f4

Browse files
committed
doc: add HttpJsonBodyParser documentation
1 parent 083f111 commit c8f38f4

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed

docs/source/nuget/packages.rst

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,69 @@ And like this for Http API::
264264
}
265265
}
266266

267+
Voxel.MiddyNet.HttpJsonBodyParser
268+
---------------------------------
269+
This package contains a middleware that parse JSON object mapping to a explicit type. There are two versions avalaible:
270+
271+
* One for REST Api (APIGatewayProxyRequest and APIGatewayProxyResponse).
272+
* And another for Http Api (APIGatewayHttpApiV2ProxyRequest and APIGatewayHttpApiV2ProxyResponse).
273+
274+
Configuration
275+
^^^^^^^^^^^^^
276+
When you use the middleware, must put the specific type to convert JSON, then the middleware adds a object under context.AdditionalContext with the key "body". To access to the typed object you must do a casting of context["body"] with the custom type.
277+
278+
Sample code
279+
^^^^^^^^^^^
280+
A typical use of the middelware will look like this for Rest API::
281+
282+
public class MySample : MiddyNet<APIGatewayProxyRequest, APIGatewayProxyResponse>
283+
{
284+
public MySample()
285+
{
286+
Use(new HttpJsonBodyParserMiddleware<Foo>());
287+
}
288+
289+
protected override async Task<APIGatewayProxyResponse> Handle(APIGatewayProxyRequest apiEvent, MiddyNetContext context)
290+
{
291+
var foo = ((Foo)context.AdditionalContext["Body"]);
292+
293+
// Do stuff with foo
294+
295+
var result = new APIGatewayProxyResponse
296+
{
297+
StatusCode = 200,
298+
Body = "hello from test"
299+
};
300+
301+
return Task.FromResult(result);
302+
}
303+
}
304+
305+
And like this for Http API::
306+
307+
public class MySample : MiddyNet<APIGatewayHttpApiV2ProxyRequest, APIGatewayHttpApiV2ProxyResponse>
308+
{
309+
public MySample()
310+
{
311+
Use(new HttpV2JsonBodyParserMiddleware<Foo>());
312+
}
313+
314+
protected override async Task<APIGatewayHttpApiV2ProxyResponse> Handle(APIGatewayHttpApiV2ProxyResponse apiEvent, MiddyNetContext context)
315+
{
316+
var foo = ((Foo)context.AdditionalContext["Body"]);
317+
318+
// Do stuff with typed foo
319+
320+
var result = new APIGatewayHttpApiV2ProxyResponse
321+
{
322+
StatusCode = 200,
323+
Body = "hello from test"
324+
};
325+
326+
return Task.FromResult(result);
327+
}
328+
}
329+
267330
Voxel.MiddyNet.ProblemDetailsMiddleware
268331
---------------------------------------
269332
The middleware contained in this package formats Api exceptions as ProblemDetails following [RFC7807](https://tools.ietf.org/html/rfc7807).

0 commit comments

Comments
 (0)