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 /
  • Help Room /
avatar image
0
Question by milanow · Jun 30, 2016 at 08:06 PM · networkingwwwjsonwebrequest

Issue with WWW/UnityWebRequest when sending JSON to server

Hi all, I have a problem when using either WWW/UnityWebRequest to send JSON data to my server.

I have tested my server request API on Postman, and it works. The server sends back text msg (like "Success"). But it doesn't work on Unity3D. I have tried both WWW and UnityWebRequest to send my JSON data. The code shows below, (I only posted UnityWebRequest one),

btw: the JSON I want to send is {"data", AnotherJSON}, thus I AddField by AddField("data", jsonstr)

  IEnumerator PostData(){
             User usr = new User();
             usr.name = "abc";
             usr.email = "abc@gmail.com";
             usr.age_range = 23;
             usr.gender = "male";
             usr.location = "U.S.";
             string jsonStr = JsonConvert.SerializeObject(usr);
     
             WWWForm form = new WWWForm();
             form.AddField("data", jsonStr);
             //Dictionary<string, string> headers = form.headers;
             //headers["Content-Type"] = "application/json";
             UnityWebRequest uwr = UnityWebRequest.Post(addUerURL, form);
             uwr.SetRequestHeader("Content-Type", "application/json");
             yield return uwr.Send();
     
             string s = uwr.downloadHandler.text;
             if(uwr.isError){
                 Debug.Log(uwr.error);
             }else{
                 Debug.Log("Success? " + s);
             }
 }

The User class is just a public class contains basic info using default constructor. The output goes into "No Error" and prints "Sucess? 400 Bad Request" which indicates my request are malformed.

And when I use "WWW www" as sending my request, I got the same, 400 Bad Request.

Anyone can help me out???? A lot thanks in advance.

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
1

Answer by milanow · Jun 30, 2016 at 09:39 PM

For those want the answer immediately, I got it works not using Unity's Networking, but System.Net. Using HttpWebRequest, this link helps alot How to post JSON to server. And you may encounter "417 expectation failed", in which case you need this.

But I still want to know how Unity3D works. Or maybe it's because I used UnityEngine.Experiemental.Networking? I am currently using 5.3.5f1, not gonna to update to 5.4.0

Comment
Add comment · Show 4 · 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 Olly · Jul 02, 2016 at 08:04 AM 0
Share

Do you know if HttpWebRequest will work across platforms though?

avatar image milanow Olly · Jul 06, 2016 at 05:51 PM 0
Share

Not sure about it, I only test it on my Unity and it works when deploying on Android devices.

avatar image matias_arje Olly · Aug 09, 2016 at 05:09 AM 0
Share

System.Net.HttpWebRequest does not support IPv6, which is an App Store requirement nowadays, so effectively it cannot be used on iOS devices.

avatar image Suvitruf · Aug 05, 2016 at 12:03 PM 1
Share

System.Net doesn't have support in WebGL build.

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

82 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

Related Questions

Provide x509 client certificate in request 0 Answers

How can I continuously receive the location of game object from 000webhost database? 0 Answers

Can WWW return a JOSN string? 0 Answers

UnityWebRequest returns an empty string 1 Answer

UnityWebRequest.downloadProgress keep coming back 1 Answer


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