user11303071
user11303071

Reputation: 41

Slack api request serialization

Somehow the model is not deserialized, but if change SlackResponse class to string, then inside will be raw json, as it may have settings from JSON.Net what would she normally deserializable?

Input data

payload={\"type\":\"message_action\",\"token\":\"000\",\"action_ts\":\"000\",\"team\":{\"id\":\"000\",\"domain\":\"000\"},\"user\":{\"id\":\"000\",\"name\":\"000\"},\"channel\":{\"id\":\"000\",\"name\":\"000\"},\"callback_id\":\"rm_create\",\"trigger_id\":\"000\",\"message_ts\":\"000\",\"message\":{\"bot_id\":\"000\",\"type\":\"message\",\"text\":\"000\",\"user\":\"000\",\"ts\":\"000\",\"team\":\"000\"},\"response_url\":\"000\"}

In C# created class

public partial class SlackResponse
{
    [JsonProperty("type")]
    public string Type { get; set; }

    [JsonProperty("token")]
    public string Token { get; set; }

    [JsonProperty("action_ts")]
    public string ActionTs { get; set; }

    [JsonProperty("team")]
    public Team Team { get; set; }

    [JsonProperty("user")]
    public Channel User { get; set; }

    [JsonProperty("channel")]
    public Channel Channel { get; set; }

    [JsonProperty("callback_id")]
    public string CallbackId { get; set; }

    [JsonProperty("trigger_id")]
    public string TriggerId { get; set; }

    [JsonProperty("message_ts")]
    public string MessageTs { get; set; }

    [JsonProperty("message")]
    public Message Message { get; set; }

    [JsonProperty("response_url")]
    public Uri ResponseUrl { get; set; }
}

public partial class Channel
{
    [JsonProperty("id")]
    public string Id { get; set; }

    [JsonProperty("name")]
    public string Name { get; set; }
}

public partial class Message
{
    [JsonProperty("client_msg_id")]
    public Guid ClientMsgId { get; set; }

    [JsonProperty("type")]
    public string Type { get; set; }

    [JsonProperty("text")]
    public string Text { get; set; }

    [JsonProperty("user")]
    public string User { get; set; }

    [JsonProperty("ts")]
    public string Ts { get; set; }

    [JsonProperty("team")]
    public string Team { get; set; }
}

public partial class Team
{
    [JsonProperty("id")]
    public string Id { get; set; }

    [JsonProperty("domain")]
    public string Domain { get; set; }
}

And controller method

[HttpPost]
public async void Get([FromForm(Name = "payload")] SlackResponse payload)
{
}

Upvotes: 0

Views: 427

Answers (1)

user11303071
user11303071

Reputation: 41

It seems that in asp.net core FromForm is not support serialization, the simplest solution is this

[HttpPost]
public async void Get([FromForm(Name ="payload")] string jsonString)
{
    var payload= JsonConvert.DeserializeObject<SlackResponse>(jsonString);
}

Upvotes: 1

Related Questions