Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 tdw6193 · Jul 01, 2016 at 07:19 AM · destroyjsonpostrequestrestful

POST request using WWW class with header

I am trying to make a POST request to restful web APIs in Unity.

The header would be Content-Type: application/json

An example of the raw data input is, where data is the key and json string is the value:

{"data":{"username":"name","email":"email@gmail.com","age_range":21,"gender":"male","location":"california"}}

Here's my script:

 private static readonly string POSTAddUserURL = "http://db.url.com/api/addUser";

 public WWW POST()
 {
     WWW www;

     Hashtable postHeader = new Hashtable();
     postHeader.Add("Content-Type", "application/json");

     WWWForm form = new WWWForm();
     form.AddField("data", jsonStr);
     www = new WWW(POSTAddUserURL, form);

     StartCoroutine(WaitForRequest(www));
     return www;
 }

 IEnumerator WaitForRequest(WWW data)
 {
     yield return data; // Wait until the download is done

     if (data.error != null)
     {
         MainUI.ShowDebug("There was an error sending request: " + data.error);
     }
     else
     {
         MainUI.ShowDebug("WWW Request: " + data.text);
     }
 }

How do I send the request using WWW class with both form and header? Or, just in general, how to i send this kind of post request?

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
0

Answer by aditya007 · Nov 18, 2016 at 06:19 AM

Just call WWW with a third parameter, which would be your Header . Like this :

 www = new WWW(POSTAddUserURL, form, postHeader);
Comment
Add comment · 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
0

Answer by gaminggal39 · Nov 12, 2018 at 07:49 AM

Watch this video to solve :https://www.youtube.com/watch?v=FChVgb6EXGk&t=133s

Comment
Add comment · 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

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

8 People are following this question.

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

Related Questions

Problem deserialize Json api rest 1 Answer

Webrequest Post SetRequestHeader Problem 0 Answers

Post Requests without Coroutines 2 Answers

Unable to upload Image to server with MultipartFormFileSection , WWWForm and Upload Handler 0 Answers

Posting JSON-RPC with WebRequest 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