title: Slack-Integration in ASP.NET Web-Api 2
body: <p>I want to know exactly why this is not working:</p>
  
  <pre><code>[HttpPost]
  public IHttpActionResult Post(Slack_Webhook json)
  {
      return Ok(json.challenge);
  }
  
  public class Slack_Webhook
  {
      public string type { get; set; }
      public string token { get; set; }
      public string challenge { get; set; }
  }
  </code></pre>
  
  <p>The Official <a href="https://api.slack.com/events/url_verification" rel="nofollow noreferrer">Documentation</a> says:</p>
  
  <blockquote>
    <p>We’ll send HTTP POST requests to this URL when events occur. As soon
    as you enter a URL, we’ll send a request with a challenge parameter,
    and your endpoint must respond with the challenge value.</p>
  </blockquote>
  
  <p>This is an example object (JSON) sent by Slack:</p>
  
  <pre><code>{
      "token": "Jhj5dZrVaK7ZwHHjRyZWjbDl",
      "challenge": "3eZbrw1aBm2rZgRNFdxV2595E9CY3gmdALWMmHkvFXO7tYXAYM8P",
      "type": "url_verification"
  }
  </code></pre>
  
  <p><strong>EDIT:</strong>
  I could write a book on code that does not work in this issue... here's another example that did not work - still no idea what is wrong:</p>
  
  <pre><code>[HttpPost]
  public IHttpActionResult Post()
  {
      var pairs = Request.GetQueryNameValuePairs();
      bool isValidToken = false;
      string c = "This does not work.";
      foreach(var pair in pairs)
      {
          if (pair.Key == "token")
          {
              if (pair.Value == "&lt;UNIQUETOKEN&gt;")
              {
                  isValidToken = true;
              }
          }
          if (pair.Key == "challenge")
          {
              c = pair.Value;
          }
      }
      if (isValidToken == true)
      {
          return Json(new {challenge = c });
      }
      else
      {
          return BadRequest();
      }
  }
  </code></pre>
  
  <p><strong>EDIT2:</strong>
  Very interesting that I get NULL as a response from below code - that means the body of the received POST is empty.. Could anyone with a working Slack-Integration try that out? So their site is wrong, stating the challenge is sent in the body - where else could it be?</p>
  
  <pre><code>// POST: api/Slack
  [HttpPost]
  public IHttpActionResult Post([FromBody]string json)
  {
      return Json(json);
  }
  </code></pre>
  
  <p><strong>EDIT3:</strong>
  This function is used to get the raw request, but there is nothing inside the body - I am out of solutions.. the support of Slack said, they have no idea about ASP.NET and I should ask here on SO for a solution. Here we are again! ;-)</p>
  
  <pre><code>[HttpPost]
  public async Task&lt;IHttpActionResult&gt; ReceivePostAsync()
  {
      string rawpostdata = await RawContentReader.Read(this.Request);
      return Json(new StringContent( rawpostdata));
  }
  public class RawContentReader
  {
      public static async Task&lt;string&gt; Read(HttpRequestMessage req)
      {
          using (var contentStream = await req.Content.ReadAsStreamAsync())
          {
              contentStream.Seek(0, SeekOrigin.Begin);
              using (var sr = new StreamReader(contentStream))
              {
                  return sr.ReadToEnd();
              }
          }
      }
  }
  </code></pre>
  
  <p>The result ( as expected ) looks like this:</p>
  
  <pre><code>Our Request:
  POST
  "body": { 
       "type": "url_verification",
       "token": "&lt;token&gt;",
       "challenge": "&lt;challenge&gt;"
  }
  Your Response:
  "code": 200
  "error": "challenge_failed"
  "body": {
   {"Headers":[{"Key":"Content-Type","Value":["text/plain; charset=utf-8"]}]} 
  }
  </code></pre>
  
  <p>I think I'm missing something - is there another way to get the body of the POST-Request? I mean, I can get everything else - except the body ( or it says it is empty).</p>
  
  <p><strong>EDIT4:</strong>
  I tried to read the body with another function I found - without success, returns empty string - but to let you know what I already tried, here it is:</p>
  
  <pre><code>[HttpPost]
  public IHttpActionResult ReceivePost()
  {
      var bodyStream = new 
      StreamReader(HttpContext.Current.Request.InputStream);
      bodyStream.BaseStream.Seek(0, SeekOrigin.Begin);
      var bodyText = bodyStream.ReadToEnd();
      return Json(bodyText);
  }
  </code></pre>
  
  <p>While trying to solve this I learnt a lot - but this one seems to be so impossible, that I think I will never solve it alone. Thousands of tries with thousands of different functions - I have tried hundreds of parameters and functions in all of WebApi / ASP.NET / MVC / whatever - why is there no BODY? Does it exist? What's his/her name? Where does it live? I really wanna hang out with that parameter if I ever find it, must be hidden at the end of the rainbow under a pot of gold.</p>