0

我正在使用以下 API 将文件上传到 watson 发现服务。尽管我提供了有效的类型,但我收到的响应是不受支持的文件格式。API:发现服务api

 public async Task<ActionResult> Index()
    {
        using (var httpClient = new HttpClient())
        {

            //ADD BASIC AUTH
            var authByteArray = Encoding.ASCII.GetBytes("{auth key}");
            var authString = Convert.ToBase64String(authByteArray);
            httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", authString);

            var text = string.Empty;
            var uri = "https://gateway.watsonplatform.net/discovery/api/v1/environments/{envid}/collections/{collectionid}/documents?version=2017-11-07";


            var content = new MultipartFormDataContent();
            var bytes = System.IO.File.ReadAllBytes(Server.MapPath("~/Views/UploadDocument/civilwar-api1.html"));
            var file = new StreamContent(new MemoryStream(bytes));
            content.Headers.ContentType = MediaTypeHeaderValue.Parse("text/html");
            content.Add(new StreamContent(new MemoryStream(bytes)), "file");

            var response = await httpClient.PostAsync(uri, content);
            var text1 = await response.Content.ReadAsStringAsync();

        }

        return View();
    }

api响应为:{ "code" : 415, "error" : "Unsupported Media Type" }

4

2 回答 2

1

正如您在 Watson Developer Cloud 中看到的,您可以使用.NET SDK

在此存储库中,您可以看到使用 Watson 的每个服务的示例

添加文档的方法:

#region Documents
        private void AddDocument()
        {
            Console.WriteLine(string.Format("\nCalling AddDocument()..."));
            using (FileStream fs = File.OpenRead(_filepathToIngest))
            {
                var result = _discovery.AddDocument(_createdEnvironmentId, _createdCollectionId, _createdConfigurationId, fs as Stream, _metadata);

                if (result != null)
                {
                    Console.WriteLine(JsonConvert.SerializeObject(result, Formatting.Indented));
                    _createdDocumentId = result.DocumentId;
                }
                else
                {
                    Console.WriteLine("result is null.");
                }
            }
        }
于 2017-12-19T12:01:59.150 回答
0

尝试httpClient.DefaultRequestHeaders .Accept .Add(new MediaTypeWithQualityHeaderValue("application/json"));//ACCEPT header

于 2017-12-18T12:48:16.207 回答