Azure Function中的POST请求|不支持的媒体类型

时间:2018-07-30 13:02:56

标签: c# asp.net azure httpclient

我正在编写一个Azure函数,它将点击POST API来注册用户。但是我收到以下错误:

{状态代码:415,ReasonPhrase:'不受支持的媒体类型',版本:1.1,内容:System.Net.Http.StreamContent,标头: {   连接:保持活动状态   连接:保持活动   保持活动:超时= 5,最大= 100   日期:2018年7月30日星期一12:46:09 GMT   的Set-Cookie:_csrf = 93955591540dfcd886bc71acf36448de9fasdfasdfasdfasecb5be6e3f4c696ad7c5d668a0504e2a%3A2%3A%7BI%3A0%3BS%3A5%3A%22_csrf%22%3Bi%3A1%3BS%3A32%3A%22SODw5mrPJVkrA0ujupBXezlrXLS796-S%22%3B%7D;路径= /; httponly   伺服器:Apache / 2.4.25   伺服器:(Win32)   伺服器:PHP / 5.6.30   X-Powered-By:PHP / 5.6.30   内容长度:85   内容类型:application / json;字符集= UTF-8 }}

以下是我编写的代码:

namespace AzureFunctionApp1
{

public static class Registration
{
    [FunctionName("Registration")]
    public static async Task<HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)]HttpRequestMessage req, TraceWriter log)
    {
        log.Info("C# HTTP trigger function processed a request.");

        using (HttpClient client = new HttpClient())
        {
            string resultJson = "";
            string token = "tokenValue";
            var inboundReqContent = req.Content;
            User userObj = inboundReqContent.ReadAsAsync<User>().Result;
            string jsonString = JsonConvert.SerializeObject(userObj);
            string requestUrl = "URL";

            // Setting request header
            client.BaseAddress = new Uri(requestUrl);
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Add("Authorization", "Bearer " + token);
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            var wrappedContent = new StringContent(jsonString, Encoding.UTF8, "application/json");
            HttpResponseMessage response = await client.PostAsync(client.BaseAddress, wrappedContent);


            if (response.IsSuccessStatusCode)
            {
                HttpContent responseContent = response.Content;
                var result = await responseContent.ReadAsStringAsync();
                resultJson = Newtonsoft.Json.JsonConvert.DeserializeObject(result).ToString();
                return req.CreateResponse(HttpStatusCode.OK, resultJson);
            }
            return req.CreateResponse(HttpStatusCode.BadRequest, response);
        }
    }
}

public class User
{
    public string branch_id { get; set; }
    public string fullName { get; set; }
    public string fatherName { get; set; }
    public string mobileNo { get; set; }
    public string cnic { get; set; }
    public string email { get; set; }
    public string dateofbirth { get; set; }
    public string address { get; set; }
    public string maritalstatus { get; set; }
    public string bloodGroup { get; set; }
    public string city { get; set; }
    public string gender { get; set; }
    public string salutation { get; set; }
    public string ref_no { get; set; }
    public string passport { get; set; }

}
}

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

检查Postman是否报告以Content-Type:text / plain返回响应。我曾经明确指出响应是Content-Type:application / json,如以下代码片段所示:

return req.CreateResponse(HttpStatusCode.OK, resultJson, "application/json");

请注意,您可以在第二个参数中传递返回类型的实例,CreateResponse()将对其进行JSON序列化,因此您可以省略上一行的序列化。

HTH