- Home /
The question is answered, right answer was accepted
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 !
Follow this Question
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