正在使用邮递员向 API 发出 post 请求,我收到 json 响应,当我尝试发出 post 请求时,我收到字符串响应

时间:2021-04-20 17:50:25

标签: c# asp.net-web-api

  public async Task <JObject> PostOcr1()
    {
        var client = new HttpClient();

        var request = new HttpRequestMessage
        {
            Method = HttpMethod.Post,
            RequestUri = new Uri("https://microsoft-computer-vision3.p.rapidapi.com/ocr?detectOrientation=true&language=ar"),
            Headers =
{
    { "x-rapidapi-key", "8830b90506msha145d86eb92ef41p16ff4djsn74eca27d491" },
    { "x-rapidapi-host", "microsoft-computer-vision3.p.rapidapi.com" },
},
            Content = new StringContent("{\r\n    \"url\": \"https://scontent.fcai19-1.fna.fbcdn.net/v/t1.18169-9/14040173_1167189816673874_4131574684799363249_n.png?_nc_cat=102&ccb=1-3&_nc_sid=973b4a&_nc_ohc=Ovfjjq2erC4AX-r8M5k&_nc_ht=scontent.fcai19-1.fna&oh=7d8d425c6cbcbef95cc820e45fa158f3&oe=60A167BA\"\r\n}")
            //   Content = new StringContent("{\r\n    \"url\": \"https://answers.opencv.org/upfiles/15430864595779441.jpg\"\r\n}")

            {
                Headers =
    {
        ContentType = new MediaTypeHeaderValue("application/json")
    }
            }
        };
        using (var response = await client.SendAsync(request))
        {
            response.EnsureSuccessStatusCode();
              var body = await response.Content.ReadAsStringAsync();

            JObject joResponse = JObject.Parse(body);
            
            return body;
        }
    }

我不想将响应转换为字符串 我认为问题出在那条线上。 var body = await response.Content.ReadAsStringAsync();

有没有办法得到和邮递员一样的回复(json)

1 个答案:

答案 0 :(得分:0)

您遇到错误了吗?您正在将响应作为字符串“response.Contenet.ReadAsStringAsync()”读取并将其转换为 JObject。您已经在获取应用程序/json。所以我想只要做一个 JObject = response.content 就行了。这是使用 http 请求的一种非正统方式。通常,您会将响应反序列化为 cs 对象。阅读相关内容和 REST API 将帮助您更好地了解正在发生的事情。

相关问题