我正在使用以下API將文件上傳到watson發現服務。即使我提供了一個有效的類型,我也會得到不支持的文件格式的響應。 API:discovery service api如何通過API將文檔上傳至watson發現服務。 ASP.net
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" }
儘管代碼只有答案才能解決問題,但有些解釋在理解解決方案以及將來如何解決類似問題方面有很長的路要走。 –