空的Request.Form使用$ http angular发布

时间:2014-05-06 18:48:10

标签: c# angularjs

无法从C#中的Request中获取数据。我所拥有的是角度$ http发布到代理api。问题是Request.Form是空的; Request.InputStream也一样。

角:

$http({
                method: "POST",
                url: '/apiproxy/projects/' + data.Project.Id + '/recruiting-groups',
                data: angular.toJson(group, false)
            });

C#:

 public ActionResult Index(string pathInfo)
        {
            var url = Settings.GetValue<string>("QualService") + "/" + pathInfo + "?" + Request.QueryString;


            //Get stuff from the back end
            using (var client = new WebClient())
            {
                client.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
                client.Headers[HttpRequestHeader.Cookie] = Request.Headers["Cookie"];
                client.Headers[HttpRequestHeader.Authorization] = "Basic " +
                                                                  Convert.ToBase64String(
                                                                      Encoding.UTF8.GetBytes(
                                                                          "x:{0}".Fmt(UserSession.ApiKey)));


                try
                {
                    var responseBytes = Request.HttpMethod == "POST" ? client.UploadValues(url, Request.Form) : client.DownloadData(url);

                    var result = new ContentResult();
                    result.Content = Encoding.UTF8.GetString(responseBytes);
                    result.ContentEncoding = Encoding.UTF8;
                    result.ContentType = "application/json";
                    return result;
                }
                catch(Exception e)
                {
                    Logger.Error("Error while proxying to the API:  ", e);
                }
            }

            return Json(false);
        }

当我查看chrome中的网络选项卡时,我可以看到传递的数据: PostedData

尽管Request.Form为null,但无论如何。就此而言,Request.InputStream为空。发生了什么事?

1 个答案:

答案 0 :(得分:0)

您可能想尝试添加以下标头类型之一:

application / x-www-form-urlencoded multipart / form-data