本文介绍了检索http请求C#的json对象的主体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,我无法检索JSON对象的POST语句的主体.这是执行http-Request时调用的函数:

I have the problem, that I can't retrieve the body of a POST statement of a JSON Object. Here is the function, which is called when executing the http-Request:

        public static HttpResponseMessage Run([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = "HttpTriggerCSharp/name/{name}")]HttpRequestMessage req, string name, TraceWriter log)
    {
        log.Info("C# HTTP trigger function processed a request.");

        string output = JsonConvert.SerializeObject(req.Content.ToString());

        // Fetching the name from the path parameter in the request URL
        return req.CreateResponse(HttpStatusCode.OK, output);

    }

我正在使用Postman和以下URL执行POST: http://localhost:7071/api/HttpTriggerCSharp/name/test

Im executing the POST with Postman and the following URL: http://localhost:7071/api/HttpTriggerCSharp/name/test

在标题中,我写了"Content-Type:application/json",正文看起来像这样:

In the Header I wrote "Content-Type: application/json" and the Body looks like this:

  {
   "Benutzer":"Nenad",
   "Passwort":"test"
  }

我的结果是这样的:"\" System.Net.Http.StreamContent \"

My result is this: "\"System.Net.Http.StreamContent\""

谢谢您的帮助!

推荐答案

感谢大家的回答,

我现在在朋友的帮助下找到了另一个解决方案,如下所示:

i found now an other solution with the help of a friend, this is the following:

        public static async System.Threading.Tasks.Task<HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = "HttpTriggerCSharp/name/{name}")]HttpRequestMessage req, string name, TraceWriter log)
    {
        log.Info("C# HTTP trigger function processed a request.");

        dynamic dataArray = await req.Content.ReadAsAsync<object>();

        string output = dataArray.ToString();

        var data = Newtonsoft.Json.JsonConvert.DeserializeObject<Benutzer>(output);

        // Fetching the name from the path parameter in the request URL
        return req.CreateResponse(HttpStatusCode.OK, data);

    }

这篇关于检索http请求C#的json对象的主体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 18:27