Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
3
Question by Fattie · Apr 22, 2013 at 06:26 PM · wwwjsonaddfield

Using WWW to simply post a slab of text .. ala JSON

Normally I use WWW and AddField to simply create forms to post, essentially like

 POST /page/page
 
 name="steve"
 height="181"


however, if you just want to do this for typical modern web services:

 POST /page/page
 
 I will take care of
 all this myself
 just let me pass you the text, pls Unity

there's actually NO WAY to do that, right???

An intelligent colleague suggested this:

     byte[] dd = Encoding.ASCII.GetBytes(stuff.ToCharArray());
     WebRequest request = new WebRequest();
     request.www = new WWW(happyURL, dd);

but I have Fear. Does anyone have any suggestions on this? Thank you!

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
13
Best Answer

Answer by Bunny83 · Apr 22, 2013 at 08:02 PM

You don't have to use key-value pairs as post data. Post data can be any data which is simply appended on the request headers. On the server side it's up to you to parse the incoming data properly. PHP for example will parse key-value pairs automatically into an associative array.

An HTTP request is actually very simple. It may consists of 3 major parts:

  • The actual request which just contains the method, the path of the requested resource and the used protocol

  • A list of optional header fields

  • Optional post-data

The HTTP header (which consists of the first request line and all header fields) is finished by an empty line. Everything that follows the second CR / LF pair is post data.

The general layout:

    GET /path/to/resource HTTP/1.1
    Header-Field-Name: Value
    Header-Field-Name: Value
    
    Post data .......
    All the way until the transfer is aborted

Unity's WWW class allows you to define almost everything "manually".

Let's take a look at the actual WWW constructor, all others are just using this one:

     new WWW(URL, postData, requestHeaders);

Those are:

  • URL = a complete URL. The domain part is used to determine the IP to which server the request should be sent. The path information goes into the first request line

  • postData = arbitrary data which is simply appended at the end. The WWW class uses this to determine whether to use "GET" or "POST". If the post data is "null" it will perform a GET if it's set to an byte array it will send a POST request.

  • requestHeaders = a Hashtable which contains the desired HTTP request headers.

Here are the two limitations of the WWW class:

  • You can't append any post data on a GET request. It actually doesn't make much sense since all HTTP servers will ignore it, but technically it's a limitation.

  • You can only add each HTTP header once. This is actually against the HTTP standard. This limitation comes from the fact that Unity uses a Hashtable for storing the headers. A hashtable can't have two or more identical keys.

The WWWForm class is just a helper class to create the post data and the request headers for a "Web form" but it's only useful when you want to send url-encoded data. The headers generated by WWWForm are, well, very simple:

 public Hashtable headers
 {
     get
     {
         Hashtable hashtable = new Hashtable();
         if (this.containsFiles)
         {
             hashtable["Content-Type"] = "multipart/form-data; boundary=\"" + Encoding.UTF8.GetString(this.boundary) + "\"";
         }
         else
         {
             hashtable["Content-Type"] = "application/x-www-form-urlencoded";
         }
         return hashtable;
     }
 }

This is all. The WWWForm just selects the correct Content-Type depending on if you added binary form data or not. But again you don't have to use WWWForm and you don't have to use url-encoded data.

So here's an example how to send a fully customized POST request:

 //C#
 string ourPostData = "{\"someJSON\":42}";
 
 Hashtable headers = new Hashtable();
 headers.Add("Content-Type", "application/json");
 headers.Add("Cookie", "Our session cookie");
 
 byte[] pData = Encoding.ASCII.GetBytes(ourPostData.ToCharArray());
 
 WWW www = new WWW("http://our.server.domain.name/path/to/our/resource/file", pData, headers);

This would produce a request like this:

     POST /path/to/our/resource/file HTTP/1.1
     Content-Type: application/json
     Cookie: Our session cookie
     
     {"someJSON":42}


Comment
Add comment · Show 11 · 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 · Apr 22, 2013 at 08:31 PM 1
Share

I'm not sure if Unity will add an "Content-Length" header automatically when you add post data, but usually a Content-Length header must be included when you post and additional data since the server doesn't know how much data will follow. It usually just waits until the connection is closed, but it's better to tell the server how much data he has to expect.

avatar image Bunny83 · Apr 22, 2013 at 08:39 PM 1
Share

Btw to read the posted data in PHP see this SO question.

It's actually quite shocking that ~90% of the PHP users think post data always have to be url encoded data. Unfortunately there's very little information on raw post data.

avatar image Bunny83 · Apr 23, 2013 at 07:48 AM 1
Share

A RESTful communication just follows some rules. For example that the communication is stateless on the server side. So all data required to precess the request have to be transmitted by the client, each request.

Ajax is just a name for the technique to "manually" executing sub-requests from a loaded webpage and process the results on the client side. From HTTP view it's just another request ;)

Your example of Content-Length looks right, however as i said i'm not sure if Unity add this field "automatically". I would have to try this with WireShark to see if it's added.

avatar image Bunny83 · Apr 23, 2013 at 08:57 AM 1
Share

Thanks for all the upvotes, but don't forget the question ;)

avatar image Bunny83 · May 03, 2013 at 08:06 AM 1
Share

Do you want a GET or POST request? Because Unity deter$$anonymous$$es which one it should use on the fact if there is post data available or not. If there is no post data Unity performs a GET request. Otherwise a POST. If the server doesn't expect any post data but you need to do a POST it doesn't hurt when you append one byte garbage post data ;)

Show more comments

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

14 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

Related Questions

JsonUtility deserialization via JSON file doesn't work on WWW, works on File.ReadAllText 1 Answer

json (LitJSON) from PHP array.. How? 0 Answers

Use the data received from WWW in UI 1 Answer

Loaclization in WebGL? 1 Answer

Cannot cast from source type to destination type Json 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