• 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
1
Question by siddharth3322 · Jun 29, 2016 at 10:29 AM · unity 5wwwuploadwwwformpost

Blank web server response in multiple type data submission

When I attach image resources to web service api call, its giving me blank server side response as well nothing submitted to server as well.

I was using following code for this purpose:

 IEnumerator RegisterVoterProfile ()
     {
         WWW localFile = new WWW ("file:///Users/gaminguruz/Desktop/messi.jpg");
         yield return localFile;
 
         if (localFile.error == null)
             Debug.Log ("Loaded file successfully");
         else {
             Debug.Log ("Open file error: " + localFile.error);
             yield break; // stop the coroutine here
         }
 
         Dictionary<string,string> headerDisc = new Dictionary<string, string> ();
 //        headerDisc.Add ("Content-Type", "multipart/form-data");
         headerDisc.Add ("Api-Key", "Api-Key Paste Here");
 
         WWWForm form = new WWWForm ();
         form.AddField (GameConstants.REGISTRATION_USERNAME_ARG, userNameInput.text);
         form.AddField (GameConstants.REGISTRATION_PASSWORD_ARG, passwordInput.text);
         form.AddField (GameConstants.REGISTRATION_EMAIL_ARG, emailInput.text);
         form.AddBinaryData (GameConstants.REGISTRATION_PROFILE_PIC_ARG, localFile.bytes);
         form.AddField (GameConstants.REGISTRATION_USERTYPE_ARG, GameConstants.VOTER_USER_TYPE);
         byte[] rawData = form.data;
 
         WWW www = new WWW (GameConstants.REGISTRATION_BASE_URL, rawData, headerDisc);
         yield return www;
 
         if (www.error == null) 
             Debug.Log ("Data: " + www.text);    
         else 
             Debug.Log ("Error: " + www.error);
     }

Attaching output image too:

alt text

If I remove following line from source code then data get submitted over and get proper response as well. Even if I just upload image only then also task completed successfully.

 form.AddBinaryData (GameConstants.REGISTRATION_PROFILE_PIC_ARG, localFile.bytes);

Even in PostMan and DHC all things working perfectly. alt text

Now at which place, I was doing mistake that I can't able to find so please help me in this.

screen-shot-2016-06-29-at-35458-pm.png (23.3 kB)
screen-shot-2016-06-29-at-40553-pm.png (153.9 kB)
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

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Bunny83 · Jun 29, 2016 at 12:44 PM

The way you setup your headers is a bit wrong. The WWWForm class will setup the basic header information which you ignore since you pass your own header dictionary. The general procedure when applying cusom headers is this:

 WWWForm form = new WWWForm();
 // add your form data here
 form.AddField(...);
 // [...]
 
 var headers = form.headers;
 // add your custom headers here
 headers.Add(..., ...);
 
 // Send your request
 WWW www = new WWW ( ... , form.data, headers);
 yield return www;


The default headers contain the proper "content type" which is determined from the actual form data. A multipart content type needs a boundary marker which is created by the WWWForm class and is used to seperate the different parts. The boundary marker is a random 40 character string that is generated in the WWWForm internally. When reading the "data" property the WWWForm uses this internally generated boundary marker to seperate the parrs. The content type header need the same boundary marker or the request won't work.

The "headers" property of the WWWForm class looks like this:

 public Dictionary<string, string> headers
 {
     get
     {
         Dictionary<string, string> dictionary = new Dictionary<string, string>();
         if (this.containsFiles)
         {
             dictionary["Content-Type"] = "multipart/form-data; boundary=\"" + Encoding.UTF8.GetString(this.boundary, 0, this.boundary.Length) + "\"";
         }
         else
         {
             dictionary["Content-Type"] = "application/x-www-form-urlencoded";
         }
         return dictionary;
     }
 }


Comment
Add comment · Show 3 · 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 siddharth3322 · Jun 29, 2016 at 04:53 PM 0
Share

@Bunny83, Really thanks for your reply :) Now I have one question in this too, I have now changed my code to some thing like this,

 WWWForm form = new WWWForm ();
         form.AddField (GameConstants.REGISTRATION_USERNA$$anonymous$$E_ARG, userNameInput.text);
         form.AddField (GameConstants.REGISTRATION_PASSWORD_ARG, passwordInput.text);
         form.AddField (GameConstants.REGISTRATION_E$$anonymous$$AIL_ARG, emailInput.text);
         form.AddBinaryData (GameConstants.REGISTRATION_PROFILE_PIC_ARG, localFile.bytes, "virat.jpg", "image/jpeg");
         form.AddField (GameConstants.REGISTRATION_USERTYPE_ARG, GameConstants.VOTER_USER_TYPE);
 
         Dictionary<string,string> headerDisc = form.headers;
         headerDisc ["Content-Type"] = "multipart/form-data; boundary=\"" + Encoding.UTF8.GetString (this.boundary, 0, this.boundary.Length) + "\"";
 
         WWW www = new WWW ("http://photo-competition.basketballtriviaguru.com/restapi/register_demo", form.data, headerDisc);
 //        WWW www = new WWW (GameConstants.REGISTRATION_BASE_URL, rawData, headerDisc);
         yield return www;

So what come at place of this.boundary because my project not contains anything like this.

avatar image Bunny83 siddharth3322 · Jun 29, 2016 at 07:29 PM 1
Share

I think you misunderstood my answer ^^. You should not set the Content-Type yourself. That's the whole point in using the WWWForm class. It handles that for you. Just remove the line where you set the Content-Type.

The "boundary" field is a private field inside the WWWForm class. It's a byte array which is automatically initialized with random characters when you create the WWWForm. Putting together the post data for a multipart content isn't that difficult, just a bit tedious. Here's the code that actually builds the post data (just for reference). There the internal boundary field is used to put the different parts together.

Though when you use the WWWForm class you shouldn't care about the actual implementation.

avatar image siddharth3322 Bunny83 · Jun 30, 2016 at 03:57 PM 0
Share

@Binny83 thanks for this awesome reply. I got your point for this. Now my updated code.

 WWWForm form = new WWWForm ();
         form.AddField (GameConstants.REGISTRATION_USERNA$$anonymous$$E_ARG, userNameInput.text);
         form.AddField (GameConstants.REGISTRATION_PASSWORD_ARG, passwordInput.text);
 
         Dictionary<string,string> headerDisc = form.headers;
         headerDisc.Add ("Api-$$anonymous$$ey", "Your API $$anonymous$$ey");
 
         WWW www = new WWW (GameConstants.UPDATE_REGISTRATION_BASE_URL, form.data, headerDisc);
         yield return www;

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

79 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

Related Questions

Upload large files 0 Answers

How do I post a cURL request from Unity? 1 Answer

HTTP requests in Unity 5 0 Answers

How to Upload multiple files to a server using UnityWebRequest.Post(); 2 Answers

GET Request Wrapper 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges