i need to integrate watson discovery features into existing windows service application developed with visual studio 2015 framework 4.5.
we cannot upgrade the framework version, so nuget library ibm.watson is not working.
i try to convert this curl command
curl -X POST -u "{username}":"{password}" -F "file=@c:\temp\1.json" https://gateway-fra.watsonplatform.net/discovery/api/v1/environments/{environment}/collections/{collection}/documents?version=2017-11-07
into c# code:
void test
{
ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
string username = "{username}";
string password = "{password}";
string postData = File.ReadAllText(@"c:\temp\1.json");
string BASE_URL = "https://gateway-fra.watsonplatform.net/discovery/api/";
string url = BASE_URL + "/v1/environments/{environment}/collections/{collection}/documents";
var request = (HttpWebRequest)WebRequest.Create(url);
var data = Encoding.ASCII.GetBytes(postData);
request.Method = "POST";
request.ContentType = "application/json";
request.ContentLength = data.Length;
request.Credentials = new NetworkCredential(username, password);
using (var stream = request.GetRequestStream())
{
stream.Write(data, 0, data.Length);
}
var response = (HttpWebResponse)request.GetResponse();
var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
catch (WebException wex)
{
HttpWebResponse oHttpWebResponse = (HttpWebResponse)wex.Response;
Encoding oEncoding = Encoding.GetEncoding(1252);
string sEx = string.Empty;
using (StreamReader oStreamReader = new StreamReader(oHttpWebResponse.GetResponseStream(), oEncoding))
{
sEx = oStreamReader.ReadToEnd();
oStreamReader.Close();
}
System.Diagnostics.Debug.WriteLine(sEx);
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.ToString());
}
}
but get this error:
{
"code": 400,
"error": "The request does not contain a \"file\" part or \"metadata\" part. Include at least one of those parts and resubmit your request."
}
what's the way to add file or metadata to request?