Esempio n. 1
0
        public async Task <IHttpActionResult> Post([NakedBody] string testStr) //[FromBody] string data)
        {
            if (String.IsNullOrEmpty(testStr))
            {
                testStr = Request.Content.ReadAsStringAsync().Result;
            }

            if (String.IsNullOrEmpty(testStr))
            {
                NameValueCollection nvc = Request.Content.ReadAsFormDataAsync().Result;
                foreach (string key in nvc)
                {
                    testStr += nvc[key].ToString();
                }
            }

            if (String.IsNullOrEmpty(testStr) && Request.Content.IsMimeMultipartContent())
            {
                Collection <HttpContent> hcc = Request.Content.ReadAsMultipartAsync().Result.Contents;
                foreach (var item in hcc)
                {
                    testStr += item.ReadAsStringAsync().Result;
                }
            }

            if (String.IsNullOrEmpty(testStr))
            {
#if DEBUG
                return(BadRequest("Empty Input String"));
#else
                return(Ok("0"));
#endif
            }
            else
            {
                try
                {
                    if (Request.Content.Headers.ContentType.MediaType.ToUpper().Contains("URLENCODED"))
                    {
                        testStr = WebUtility.UrlDecode(testStr);
                    }
                    Grouper grpr = new Grouper(testStr.ToString());
                    return(Ok(grpr.score().ToString()));
                }
                catch (Exception e)
                {
                    return(BadRequest("Error with the supplied data:\r\n" + e.ToString() +
                                      "\r\n" + testStr.ToString().Substring(0, (testStr.Length > 2000? 2000: testStr.Length))));
                }
            }
        }