I am constructing a file post to a REST api. The REST api is specifically looking for mulipart/form-data content. Seems like now matter how I try and override it, the content-type when it gets to the server is always “application/x-www-form-urlencoded”. Does anyone know how to send content as “multipart/form-data”?
byte[] data = File.ReadAllBytes (@"<file location>");
List<IMultipartFormSection> postForm = new List<IMultipartFormSection> ();
postForm.Add (new MultipartFormDataSection ("SectionHeader", "data", "text/plain"));
postForm.Add (new MultipartFormDataSection ("SectionHeader", "data", "text/plain"));
postForm.Add (new MultipartFormDataSection ("SectionHeader", "data", "text/plain"));
postForm.Add (new MultipartFormDataSection ("SectionHeader", "data", "text/plain"));
postForm.Add (new MultipartFormDataSection ("SectionHeader", "data", "text/plain"));
postForm.Add (new MultipartFormDataSection ("SectionHeader", "data", "text/plain"));
postForm.Add(new MultipartFormFileSection("SectionHeader", data, "data", "application/octet-stream"));
string boundaryString = "---NextPart_" + Guid.NewGuid ().ToString () + "---";
byte[] boundary = Encoding.ASCII.GetBytes (boundaryString);
UnityWebRequest www = UnityWebRequest.Post(baseApiUrl + dataApiUrl, postForm, boundary);
www.uploadHandler.contentType = "multipart/form-data";
www.SetRequestHeader ("Authorization", completeToken.token_type + " " + completeToken.access_token);
yield return www.Send();
if(www.isError) {
Debug.LogError(www.error);
}
else if (www.responseCode != 200) {
<report responsecode and associated server/client error>
//This is where I am getting 415 Unsupported Media Type
}
else{
Debug.Log("Form upload complete!");
}