Home > database >  C# Type object as payload to Post Method
C# Type object as payload to Post Method

Time:09-13

Can we upload a C# Type object to a Post Method ?

    [HttpPost]
    [Route("SendModelPayload")]
    public async Task<IActionResult> SendModelPayload([FromBody] Type type)
    {
        // Do some stuff
        return Ok("test");
    }

But, Here type is null.

client code:

        HttpClient httpClient = new HttpClient();
        httpClient.BaseAddress = new Uri(@"http://localhost:5000");

        var res = httpClient.PostAsJsonAsync(@"/api/ModelPayload/SendModelPayload", 
        typeof(Student)).Result;

Any ideas, Thanks in advance !!

CodePudding user response:

When transmitting data to a REST API, you need to keep in mind that client and server are basically unrelated in terms of technology and therefore use a common ground that a lot of programming languages support. In your case, there might be a .NET client and a .NET server, but it could well be that a Javascript client calls your API that does not know of the Type class. Even if you have .NET client and a .NET server, the client could send a type that the server is unaware of.

So you need to find a common ground that is supported by various programming environments and is flexible enough to also cope with the situation that the client sends data that the server cannot map to a type.

One way would be to not transmit a Type directly, but transmit the type name as a string. The server could then try to load the type directly. If the type can be found, it could work with the type, if not, it would return a BadRequestResult to notify the client of the invalid data.

[HttpPost]
[Route("SendModelPayload")]
public async Task<IActionResult> SendModelPayload([FromBody] string typeName)
{
    // Try to load type
    var type = Type.GetType(typeName);
    if (type == null)
        return BadRequest();
    // Do some stuff
    return Ok("test");
}

The client would send a type name instead of a real type e.g.

var res = httpClient.PostAsJsonAsync(@"/api/ModelPayload/SendModelPayload", 
    typeof(Student).AssemblyQualifiedName).Result;

By transmitting the AssemblyQualifiedName, the type name is very specific, so the server needs to be able to load the exact same type, but fail if it cannot laod the type.

CodePudding user response:

If I understand your question, you want to upload a Student instance to the SendModelPayload endpoint?

If that is correct, then a couple of changes are necessary to get what you have working:

POST Method

    // changed to accept Student object in body
    [HttpPost]
    [Route("SendModelPayload")]
    public async Task<IActionResult> SendModelPayload([FromBody] Student type)
    {
        // Do some stuff
        return Ok("test");
    }

Client Code

    HttpClient httpClient = new HttpClient();
    httpClient.BaseAddress = new Uri(@"http://localhost:5000");

    // create instance and populate it with data
    var studentInstance = new Student() { Name = "Student Name" };

    // then call the post method and pass the instance of the Student class
    // that will get serialized and sent as the body
    var res = httpClient.PostAsJsonAsync(@"/api/ModelPayload/SendModelPayload", 
        studentInstance).Result;

CodePudding user response:

Instead of Expecting TYPE from body. Convert string input to Type

[HttpPost]
[Route("SendModelPayload")]
public async Task<IActionResult> SendModelPayload([FromBody] string TypeName)
{
var ty = Type.GetType(TypeName);
if(ty != null) 

Suggesting as I am not exactly aware with requirement

  •  Tags:  
  • c#
  • Related