Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by greatguss · Nov 20, 2017 at 04:21 PM · networkingwebrequestwwwformhttppost

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
2

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · Aug 05, 2018 at 07:34 AM 0
Share

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.

avatar image greatguss · Aug 27, 2018 at 04:36 AM 0
Share

Thanks for pointing that out to me.

avatar image
1

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image greatguss · Aug 27, 2018 at 04:40 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

108 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Pass Headers and Arguments in UnityWebRequest POST Method Object 1 Answer

Sending Unity Web Request Post returning empty from webhost 1 Answer

Upload large files 0 Answers

Best cross platform Http Library? 1 Answer

Unity HTTP Post WebRquest not working on some Android Devices 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges