POST and Json object examples #64
-
I am sending a post request using axios but have not been able to find documentation or examples for handling the post request with the json payload. Any pointers or example code would be appreciated. Didn't see anything in the sample project for post requests either. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
There are potentially three questions here, so I'm going to address them individually.
Creating a POST RoutePost routes are created using "Post" as the first parameter in the [RestRoute("Post", "/api/test")]
public async Task TestPost(IHttpContext context)
{
//Your code here
} Access The Request BodyYou can access the request payload using Deserialize The Request BodyThat depends largely on the library that you are going to use for deserialization. Grapevine intentionally doesn't include any JSON serialization (or XML or YAML or whatever), because it is unopinionated and we want to limit transitive dependencies that could cause conflicts or headaches for people who want to use Grapevine. Whatever you do, I would recommend abstracting that implementation away from the rest of your code so it can be easily reused and changed without undue disruption. Here is a sample JSON implementation using Model public class MyModel
{
public string SomeKey { get; set; } = "nothing";
} First, create an abstraction for your serialization/deserialization. In this example, I only include the JSON deserialization methods. using System.IO;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
namespace Sample
{
public abstract class Resource
{
public static JsonSerializerOptions SerializerOptions = new JsonSerializerOptions()
{
PropertyNameCaseInsensitive = true
};
public async Task<T> DeserializeAsync<T>(Stream stream)
{
return await DeserializeAsync<T>(stream, CancellationToken.None);
}
public async Task<T> DeserializeAsync<T>(Stream stream, CancellationToken token)
{
return await JsonSerializer.DeserializeAsync<T>(stream, SerializerOptions, token);
}
}
} Next, have your route class inherit from your abstraction. namespace Example
{
[RestResource]
public class MyResource : Resource
{
[RestRoute("Post", "/api/test")]
public async Task TestPost(IHttpContext context)
{
var model = await DeserializeAsync<MyModel>(context.Request.InputStream, context.CancellationToken);
await context.Response.SendResponseAsync($"Successfully sent {model.SomeKey} to the test route!");
}
}
} Finally, start up your server and send a POST with the corresponding JSON to the path |
Beta Was this translation helpful? Give feedback.
There are potentially three questions here, so I'm going to address them individually.
Creating a POST Route
Post routes are created using "Post" as the first parameter in the
RestRoute
attribute. (You can use any HTTP verb here that you want.)Access The Request Body
You can access the request payload using
context.Request.InputStream
. Be aware that this stream can only be read once! If you want the payload to be available to future routes, you'll want to add the deseri…