Base solution for your next web application
Starts in:
01 DAYS
01 HRS
01 MIN
01 SEC
Open Closed

How to use [FromBody] in AppService? #7188


User avatar
0
fguo created

I am using Angular + Core v7.0.0, and trying to supply an API. I need this API to read the Http Request Body. I tested it with a simple endpoint, but always get error.

Here is the test endpoint method:

public class TestAppService : AppServiceBase { public string ReceiveString([FromBody] string body) { return body; } }

I tested it on swager. Whatever the input string is, it always returns an 400 error:

{ "result": null, "targetUrl": null, "success": false, "error": { "code": 0, "message": "Your request is not valid!", "details": "The following errors were detected during validation.\r\n - Unexpected character encountered while parsing value: x. Path '', line 1, position 1.\r\n", "validationErrors": [ { "message": "Unexpected character encountered while parsing value: x. Path '', line 1, position 1.", "members": [ "" ] } ] }, "unAuthorizedRequest": false, "__abp": true }

What did I miss?

Thanks,


6 Answer(s)
  • User Avatar
    0
    maliming created
    Support Team

    hi

    Try changing the parameters of the ReceiveString method from sting to class (dto).

    Such as ReceiveStringInput, then the front end uses http body to pass json to the api.

  • User Avatar
    0
    fguo created

    Yes, if I can define the class, I don't need to use [FromBody]. This API acts as a "web hook". I cannot control what data type is in the request body from client. It may be a json object, or a xml document, or just a plan string. I've tried to replace string type with object type on input prameter. Such as :

    public class TestAppService : AppServiceBase { public object ReceiveData([FromBody] object body) { return body; } }

    It works only if client request body with json object. It still returns same error, if the request body is other types (e.g. string or xml).

  • User Avatar
    0
    maliming created
    Support Team

    This API acts as a "web hook".

    Can you share the http information for this web hook request?

    Example

    POST /test HTTP/1.1
    Host: localhost:4567
    Content-Type: application/json
    Content-Length: 6615
    
    {
      "action": "opened",
      "issue": {
        "url": "https://api.github.com/repos/octocat/Hello-World/issues/1347",
        "number": 1347,
        ...
      }
    }
    
  • User Avatar
    0
    fguo created

    I am not too sure the request http information such as Content-Type, because I connot control the client side.

    As I know so far, the request Content-Type can be: application/x-www-form-urlencoded, application/x-www-urlencoded, text/xml, application/xml, text/plain.

  • User Avatar
    0
    maliming created
    Support Team

    In order not to affect the application's model binding, the easiest way is to write a controller method. Manually read Request.Body in the method, the method does not require any parameters.

    example: https://weblog.west-wind.com/posts/2017/sep/14/accepting-raw-request-body-content-in-aspnet-core-api-controllers#Reading-Request.Body-for-Raw-Data

  • User Avatar
    0
    fguo created

    Good article!

    Thanks!