How can I get the raw body from a REST endpoint in Hasura? - hasura

I'm trying to set up a webhook using Hasura's REST interface. I want to just accept any data that's sent to the webhook without validation.
Is it possible to do something like this:
REST endpoint
http://localhost:8080/api/rest/callback
Methods available
POST
GraphQL Request
mutation MyMutation ($rawBody: String!) {
myMutation(payload: $rawBody) {
mutationResult
}
}

Related

How to create swagger documentation for dynamic request/response

We have an API application which is driven dynamically based on SQL defined metadata. This API app is a reporting application and the report is driven by what kind of request is passed into the API. This means if JSON request is passed like this -
ex 1
{
"Field1": "Value1"
"GroupBy": ["GroupByValue1", "GroupByValue2"]
}
Then it gives below resultset -
{
"GroupByValue1": "SomeValue1"
"GroupByValue2": "SomeValue2"
... other fields based on GroupBy1 and GroupBy2
}
ex 2
{
"Field1": "Value1"
"GroupBy": ["GroupByValue3"]
}
Then it gives below resultset -
{
"GroupByValue3": "SomeValue1"
... other fields based on GroupBy3
}
So the mapping of request v/s fields for that request is defined in the SQL database.
And we need to generate swagger documentation for this kind of dynamic requests. So my question is since we use swashbuckle for swagger documentation, we have to give it specific resultset based on the request being passed. Now we have APIs to give us those request/resultset relationship, but is there a way to generate documentation completely dynamically based on this API (using c# code or typescript) that gives us request/resultset relationship.
You can create dynamic Swagger documentation using IDocumentFilter.
We also use dynamic SQL metadata at this example.

Upload in Webapi ASP.NET Core json vs formdata

I'm using webapi asp.net core, initially my actions were using the [FromBody] attribute on the parameters, and my front end send a json. Now I'm implementing a screen that has upload, and I had to use FormData to send the file, and at that point I inserted my object (which I used to send as json) in my FormData. So, I needed to change my backend to accept the FormData, just remove [FromBody].
I would like use my action, both by passing formdata or passing a json, is it possible? How can implement this?
YourController{
public IHttpActionResult YourMethod([FromBody] model1, [FromForm]model2)
{
//your model1 defines json model.
//model2 defines properties for file
}
}
Something Like This

Multiple parameters send as json in body RetroFit without creating a model to hold it

I want to use RetroFit for Android but I donĀ“t like the idea of creating a model for every single request.
Can I create a Service that receive like 2 strings and convert it into a json that should go in the body of the post request?
You can create a HashMap and put it as parameter of your request method. For example, something like this:
#Body HashMap user

How to create an endpoint that accepts any content type in the request body using ASP.Net Web API

I'd like to create a generic API endpoint that a client can post text or file data to, where we won't know the content/media type of the data. It seems the framework requires a content formatter to be specified for any content-type passed in the HTTP header, or it throws an error. I don't want to have to define a formatter for every possible media type we might accept since we don't know yet what all they could include.
Is there a way to define an endpoint with a generic media type formatter, or not specify one at all? It doesn't seem to mind if I use a generic Object as my method parameter, but the framework keeps getting hung up on not being able to handle the media type without a formatter.
We don't actually need to be able to process this data, just store it (for something like a messaging system).
On a side note, I'd rather receive this data as the raw content of the request body and not use a multipart form request, but if it would make more sense to do it that way, it might be an option.
Or if you want to go even more low level than Youssef's suggestion, you can do..
public Task<HttpResponseMessage> Post(HttpRequestMessage request) {
var stream = await request.Content.ReadAsStreamAsync();
return new HttpResponseMessage(HttpStatusCode.Ok) { RequestMessage = request } ;
}
You can bypass formatters entirely by reading the content yourself. Here's an example:
public async Task Post()
{
string content = await Request.Content.ReadAsStringAsync();
// Store away the content
}
This doesn't require you to use or define any formatters at all.

Authorization in ASP.NET Web API: Return data specific to authorized user

Let's assume I implemented token based authorization with a custom filter attribute as described here.
Let's also assume, I have a controller that returns tasks:
public IEnumerable<Task> Get()
{
// return tasks for authorized user
}
Now, how would I go about returning only the tasks for the authorized user? Passing the user ID as a query parameter is not an option - it is forbidden to request the tasks of a different user.
you could enrich the HttpRouteData from your action filter and read it in the controller action. actionContext.ControllerContext.RouteData.Values.Add("UserId", someVaue );
You could also use the System.Runtime.Remoting.Messaging.CallContext class ( GetData and SetData )
In the code in the sample you linked to, they are encrypting the user's name in the token. In the filter they are getting this token from an http header, decrypting it back to the username, and querying it against an AuthorizedUserRepository.
AuthorizedUserRepository.GetUsers().First(x => x.Name == RSAClass.Decrypt(token));
You can certainly use a userid instead of the name, and work against a real repository instead of this sample one. You could either do all of this over again in the controller action or constructor, or you could pass it along the route data or some ThreadStatic property. If you want to get really fancy, you could implement claims based security and set a claim on the current thread's principal. Really it doesn't matter how you pass it along.
Ultimately you would just use this value in a where clause of a query or linq statement to filter down to the data you want the user to be allowed to access.

Resources