I am trying to make a post request, but keep getting error 400 Bad Request in the response.
The request is to upload a pdf file for a customer. The example provided in the documentation is a curl command and i'm having difficulty recreating it in C#
This is the curl example:
curl -F "metadata=<test.json" -F "payload=@test.pdf" https://api.company.com/v1/documents/customer/customer-<Customer's GUID>/upload?authentication=<token>;
In the test.json file it stores the document name, type, category and description. (JSON format).
Here is my C# code
byte[] pdfFile = File.ReadAllBytes(document path);
string document = Convert.ToBase64String(pdfFile);
Document docToUpload = new Document();
docToUpload.name = "testDoc";
docToUpload.format = "PDF";
docToUpload.description = "this is a test";
docToUpload.category = "19";
List<Document> docs = new List<Document>();
docs.Add(docToUpload);
DocumentObjects docObject = new DocumentObjects();
docObject.document = docs;
//convert object to JSON
string postData = JsonConvert.SerializeObject(docObject);
WebRequest docRequest = WebRequest.Create("https://api.company.com/v1/documents/customer/customer-"+customerGuid+"/upload?authentication=" + token);
using (var streamWriter = new StreamWriter(docRequest.GetRequestStream()))
{
streamWriter.Write(postData);
}
var responsedoc = (HttpWebResponse)docRequest.GetResponse();
using (var streamReader = new StreamReader(responsedoc.GetResponseStream()))
{
result = streamReader.ReadToEnd();
Console.WriteLine(result);
}