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 /
This question was closed Feb 05, 2018 at 07:49 PM by unity_K03NZhVNXvNJWA for the following reason:

The question is answered, right answer was accepted

avatar image
1
Question by unity_K03NZhVNXvNJWA · Feb 04, 2018 at 02:13 PM · apiloginwebrequestmethodshttpwebrequest

PATCH and DELETE methods - HTTP Requests problem

Hello ! I'm posting here today because I got a little problem.

I have to make a Login menu in Unity. There is already an API, that I didn't make, I just have to send requests from Unity and analyze the responses.

GET method is used to authenticate :

GET /api/auth * Request - first authentication (without token) - <mail> : (string) email address - <password> : (string) password

I can use WWW class that supports GET method :

     IEnumerator Auth(){
         string url = "127.0.0.1/api/auth?mail=" + mail + "&password=" + password;
 
         WWW www = new WWW(url);
 
         yield return www;
 
         if (!string.IsNullOrEmpty(www.error)){
             Debug.Log(www.error);
         } else {
             Debug.Log(www.text);
         }
     }

Tha't s working perfectly.

To request a new pass, POST Method :

POST /api/passwordRenewal * Request - <mail> : email address

I can use WWW class too :

     IEnumerator ResetPass(){
         string url = "127.0.0.1/api/passwordRenewal";
 
         WWWForm form = new WWWForm();
 
         form.AddField("mail", mail);
 
         WWW www = new WWW(url, form);
 
         yield return www;
         
         if (!string.IsNullOrEmpty(www.error)){
             Debug.Log(www.error);
         } else {
             Debug.Log(www.text);
         }
     }

That's working perfectly too.

Here is my problem :

To change the password, PATCH method is used :

PATCH /api/auth * Request - <new_pw> : (string) new password - <old_token> : (string) current token

But not supported by WWW class, so I searched and tried to use WebRequest class :

     public void ChangePass(){
         string json = JsonUtility.ToJson(infos); // infos contains new_pw and old_token
 
         WebRequest request = WebRequest.Create ("http://127.0.0.1/api/auth");  
         request.Method = "PATCH"; 
         byte[] byteArray = Encoding.UTF8.GetBytes (json);  
         request.ContentType = "application/json";  
         request.ContentLength = byteArray.Length;  
 
         Stream dataStream = request.GetRequestStream ();   
         dataStream.Write (byteArray, 0, byteArray.Length);   
         dataStream.Close ();  
 
         WebResponse response = request.GetResponse ();  
         Debug.Log(((HttpWebResponse)response).StatusDescription); 
 
         dataStream = response.GetResponseStream ();  
         StreamReader reader = new StreamReader (dataStream);  
         string responseFromServer = reader.ReadToEnd ();   
         Debug.Log(responseFromServer);  
 
         reader.Close ();  
         dataStream.Close ();  
         response.Close ();  
     }

But I got a Bad Request (400), I don't really know how to use the PATCH method

And to Logout, the DELETE method is used :

DELETE /api/auth * Request - <token> : (string) token

Here again I tried to use WebRequest class, but I don't know how to give the "token" information, I tried to "send it" but I got an error like "Can't send data with DELETE"

And with the following code :

     public void Logout(){
         string url = "http://127.0.0.1/api/auth";
 
         WebRequest request = WebRequest.Create(url);
         request.Method = "DELETE";
 
         WebResponse response = request.GetResponse();
     }

I got a bad request (400) I tried to pass the token in the url (?token=xxx)

It still don't work.

I never tried to send Http requests, and I don't know a lot about this, so I maybe missed important things ?

If someone knows how I can solve my problem, that would be great !

(I'm new to c#, I started to use it 3 months ago when I started to use Unity ..)

Thank you for your time !

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

0 Replies

  • Sort: 

Follow this Question

Answers Answers and Comments

73 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

Related Questions

Capturing and Tracing All HTTP Requests in Unity project 0 Answers

Image upload API works with postman but not working with Unity. -1 Answers

What is a good cross-platform alternative to WWW? 0 Answers

How to use UnityWebRequest to upload a File via Invision REST API 0 Answers

Unable to call external API (IBM Watson) via HTTP request? 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