- Home /
 
How to use the new IMultipartFormSection in a UnityWebRequest.Post
I'm new to online interaction and after successfully running a local server with Visual Studio Code I tried to Get/Post from within a unity project, I was able to use Get just fine, but I've been having trouble getting Post working with the IMultipartFormSection. I tried it with the WWWForm and that worked like a charm, but I would rather use the latest and greatest from unity.
The code I'm running in Mono, mostly taken from the documentation, is as fallows
 IEnumerator Start () {
         List<IMultipartFormSection> formData = new List<IMultipartFormSection>();
         formData.Add( new MultipartFormDataSection("_userName=ema&_passWord=car") );
         formData.Add( new MultipartFormFileSection("my file data", "myfile.txt") );
 
         UnityWebRequest www = UnityWebRequest.Post("http://localhost:5000/api/test", formData);
 
         yield return www.SendWebRequest();
 
         if(www.isNetworkError || www.isHttpError) {
             Debug.Log(www.error);
         }
         else {
             Debug.Log("Form upload complete!");
         }
     }
 
               But it always gives me a Generic/unknown HTTP error.
The post request that is listening in Visual Studio Code is
    [HttpPost]
         public string Post(string _userName, string _passWord)
         {
              var filter = Builders<Data>.Filter.Eq(x => x.name, _userName) &
               Builders<Data>.Filter.Eq(x => x.password, _passWord);
 
                var document = _collection.Find(filter).FirstOrDefault();
 
             if (document != null){
                 return "That is already an account";
             }
             else{
                 _collection.InsertOne(new Data(_userName, _passWord));
                 return "Profile created";
             }
         }
 
               Any help or suggestions would be greatly appreciated, alternatively a link to a video or tutorial that explains ImultipartFormSection and how to use it would be nice.
EDIT--------------------------------------------------------------------------------------------------------------------------------- With Bunny83's link I changed my servers post to look like what microsofts docs had
 public async Task<HttpResponseMessage> PostFormData()
 {
     // Check if the request contains multipart/form-data.
     if (!Request.Content.IsMimeMultipartContent())
     {
         throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
     }
 
     string root = HttpContext.Current.Server.MapPath("~/App_Data");
     var provider = new MultipartFormDataStreamProvider(root);
 
     try
     {
         // Read the form data.
         await Request.Content.ReadAsMultipartAsync(provider);
 
         // This illustrates how to get the file names.
         foreach (MultipartFileData file in provider.FileData)
         {
             Trace.WriteLine(file.Headers.ContentDisposition.FileName);
             Trace.WriteLine("Server file path: " + file.LocalFileName);
         }
         return Request.CreateResponse(HttpStatusCode.OK);
     }
     catch (System.Exception e)
     {
         return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, e);
     }
 }
 
               The good news is that it works now when I send the multipart/form-data via postman. I'm just sending a .txt file at the moment. The bad news is that when I try to hit the api with a unitywebrequest(the same code as above) on the line
 "await Request.Content.ReadAsMultipartAsync(provider);" 
 
               I get the fallowing error
 "Unexpected end of MIME multipart stream. MIME multipart message is not complete."
 
               I think I'm doing something wrong with my unity post request, but so far I haven't been able to figure it out.
Answer by GeekyMonkey · Aug 05, 2018 at 06:56 AM
Your request is sending a string and a file. But the api is expecting 2 strings.
What do you mean? He uses the last overload of $$anonymous$$ultipartFormFileSection which takes the file content as string and the (virtual) filename as string. The section itself will be anonymous, so it doesn't have an explicit name
Ohh ^^ you mean server side ^^. Yes, you're right.
Answer by Bunny83 · Aug 05, 2018 at 07:45 AM
Your issue is most likely on the server (like GeekyMonkey already pointed out). We don't know how you actually implemented your server, but i guess you use something like ASP.NET? To handle multi part form data have a look at this page. If you somehow don't use ASP.NET your issue is still on your implementation of your server.
guilty as charged, I am using ASP.NET. and thanks a lot for the link, I found it helpful and informative, I changed my server and now that seems to be working better, but the unity post is still causing problems. side note : it's a bit of an honor to have the Bunny83 reply to my question : )
Your answer