【问题标题】:RestSharp Azure Web API call fails when adding more parameters添加更多参数时,RestSharp Azure Web API 调用失败
【发布时间】:2019-05-08 12:32:12
【问题描述】:

我正在尝试使用 RestSharp 调用 Azure 计算机视觉 API,特别是 [POST] Batch Read File。下面的代码一切正常:

private void MakeBatchReadRequest(string imageFilePath)
{
    try
    {
        RestClient client = new RestClient("https://southeastasia.api.cognitive.microsoft.com/");
        client.AddDefaultHeader("Ocp-Apim-Subscription-Key", subscriptionKey);
        RestRequest request = new RestRequest("vision/v2.0/read/core/asyncBatchAnalyze", Method.POST);
        request.AddHeader("Content-Type", "application/octet-stream");

        byte[] byteData = GetImageAsByteArray(imageFilePath);
        request.AddParameter("application/octet-stream", byteData, ParameterType.RequestBody);

        RestResponse response = client.Execute(request);
        operationLocation = response.Headers.Where(x => x.Name == "Operation-Location").First.Value;
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

我不必包含参数mode,因为根据看到here 的API 文档,它是可选的,默认值是Printed,这正是我想要的。但是,如果我在请求中添加参数mode(以防我改变主意并切换到其他内容),如下所示:

private void MakeBatchReadRequest(string imageFilePath)
{
    try
    {
        RestClient client = new RestClient("https://southeastasia.api.cognitive.microsoft.com/");
        client.AddDefaultHeader("Ocp-Apim-Subscription-Key", subscriptionKey);
        RestRequest request = new RestRequest("vision/v2.0/read/core/asyncBatchAnalyze", Method.POST);
        request.AddHeader("Content-Type", "application/octet-stream");

        byte[] byteData = GetImageAsByteArray(imageFilePath);
        request.AddParameter("application/octet-stream", byteData, ParameterType.RequestBody);

        request.AddParameter("mode", "Printed") // This will cause the web API to return an error response.

        RestResponse response = client.Execute(request);
        operationLocation = response.Headers.Where(x => x.Name == "Operation-Location").First.Value;
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

API 返回响应状态码415 和状态描述Unsupported Media Type。整个 JSON 响应如下:

{
    "error": {
        "code": "BadArgument",
        "message": "Unsupported media type."
    }
}

我不太确定向请求添加简单参数会如何触发 API 的错误响应。另外我不确定为什么错误响应是Unsupported Media Type,因为我使用的是受支持的JPG 图像文件,并在请求中将其内容类型设置为application/octet-stream

任何帮助将不胜感激。

【问题讨论】:

  • 它对我有用(除了小的编译问题)。您是否有可能使用过时的 RestSharp 版本?我在 .NET Core 上使用 106.6.9 版本进行测试。
  • 我使用的是 WinForms .Net Framework 4.5 版和 RestSharp 105.2.3 版。我知道 RestSharp 版本 105.2.3 是 .Net Framework 4.5 的最高版本。让我尝试升级版本,然后我会回复你。
  • @cthrash 我很高兴地通知您,您是对的。谢谢你的帮助! :)

标签: c# json rest restsharp azure-cognitive-services


【解决方案1】:

我升级到 .Net Framework 4.6.1 版和 RestSharp 106.6.9 版,现在可以正常工作了。我想我别无选择,只能升级我的应用程序的 .Net Framework 版本,这样我就可以使用更新且错误更少的 RestSharp 版本。

【讨论】:

  • 我应该补充一点,如果您没有与 RestSharp 结婚,Microsoft 会为所有认知服务属性发布 Swagger 生成的 .NET 客户端 SDK。它支持 .NET Framework 4.5.2,并具有以具体类表示的返回类型。计算机视觉页面​​是here
  • 哦,我不知道。谢谢你的提示! :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-05-25
  • 2017-09-21
  • 2019-09-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多