0

I am trying to set up webhooks and receiving the payload in my endpoint. The payload gets send (as json) over http to my endpoint, in which I intercept the payload requestbody via the parameters in the endpoint action. The action only gets triggered if I put the type of the payload to 'object'. I tried creating models which map 1 on 1 with the JSON-payload; I even made sure it was case sensitive.

When I use my custom class (with the corresponding properties) as [FromBody] actionparameter, it does not get triggered. When I use the 'object' parametertype it does get triggered but I can't access any data. I also tried converting the object to a JObject using NewtonSoft.JSON, but this only returns the ValueKind property (which is also visible when inspecting the incoming object).

What can I do to map the data from the payload to my custom object? Or why does the endpoint not trigger when I use my custom model opposed to the object-parameter?

The endpoint when using object as parameter

    [AllowAnonymous]
    [HttpPost("{userId}/follows")]
    public void HandleReceivedFollow([FromRoute(Name = "userId")] string userId, [FromBody] object response)
    {
        Console.WriteLine(response);
        //PROCESS DATA AND SEND OVER WSS
    }

The endpoint when using my custom class as parameter

    [AllowAnonymous]
    [HttpPost("{userId}/follows")]
    public void HandleReceivedFollow([FromRoute(Name = "userId")] string userId, [FromBody] TwitchResponse<UserFollowResponse> response)
    {
        Console.WriteLine(response);
        //PROCESS DATA AND SEND OVER WSS
    }

The payload is sent from the webhook in the form of:

    {
        "data": 
        {
            "field1": 0,
            "field2": 1
        }
    }

My custom classes look like:

TwitchResponse

public class TwitchResponse<T> : ITwitchResponse<T>
{

    public T data { get; set; }

    public TwitchResponse() { }

    public TwitchResponse(T data)
    {
        this.data = data;
    }

}

UserFollowResponse

    public class UserFollowResponse
{

    public string from_id { get; set; }
    public string from_name { get; set; }
    public string to_id { get; set; }
    public string to_name { get; set; }
    public string followed_at { get; set; }

    public UserFollowResponse() { }

    public UserFollowResponse(string from_id, string from_name, string to_id, string to_name, string followed_at)
    {
        this.from_id = from_id;
        this.from_name = from_name;
        this.to_id = to_id;
        this.to_name = to_name;
        this.followed_at = followed_at;
    }

}

The payload I receive in the endpoint when I use the object-parameter:

ValueKind = Object : "{"data":[{"followed_at":"2020-07-07T11:38:16Z","from_id":"70700448","from_name":"HetDiamondSword","to_id":"166294598","to_name":"RamonPeekFifa"}]}"
TylerH
  • 20,799
  • 66
  • 75
  • 101

1 Answers1

0

I took a look at this JSON:

{
   "data":[
      {
         "followed_at":"2020-07-07T11:38:16Z",
         "from_id":"70700448",
         "from_name":"HetDiamondSword",
         "to_id":"166294598",
         "to_name":"RamonPeekFifa"
      }
   ]
}

it looks like that the "data" element is an array.

Can you modify your action so that it looks like this:

[AllowAnonymous]
    [HttpPost("{userId}/follows")]
    public void HandleReceivedFollow([FromRoute(Name = "userId")] string userId, [FromBody] TwitchResponse<IEnumerable<UserFollowResponse>> response)
    {
        Console.WriteLine(response);
        //PROCESS DATA AND SEND OVER WSS
    }

and let us know if anything changed?

ddfra
  • 2,413
  • 14
  • 24