UPDATE, DELETE PATCH www requests. How to?

Looking at the docs for WWW class Unity - Scripting API: WWW

It only mentions GET and POST requests

“The WWW class can be used to send both GET and POST requests to the server”

What about UPDATE, DELETE and other request types? How are those done?

nm, I found this:

http://docs.go-mono.com/?link=T%3ASystem.Net.WebRequest

and was able to do:

using System;
using System.Net;

public class WebRequestExample {

  public static void Main() {
    // Initialize the WebRequest.
    WebRequest myRequest =
      WebRequest.Create("http://example.com");
    myRequest.Method = "DELETE";
    WebResponse myResponse = myRequest.GetResponse();
  }
}

I know this is an old thread, but I searched elsewhere and did not see many good answers. My answer may not be great, but it is easy and it works. UnityWebRequest have a default PATCH method but it does have one for PUT, which is a similar request. You can simply initiate a UnityWebRequest.Put and then change the method to PATCH, like so:

byte[] formData;
/*
build form data here and then encode as byte[]
formData = System.Text.Encoding.UTF8.GetBytes(someStringHere);
*/

UnityWebRequest request = UnityWebRequest.Put(URL, formData);
request.method = "PATCH";


/* You may need to add header(s) */
request.SetRequestHeader("Content-Type","application/json");

Then you can use StartCoroutine() or using() {} (shown here: Unity - Scripting API: Networking.UnityWebRequest.Put) to send and yield return

2 Likes

That worked for me, thanks!

Thanks for the help, I have created this Delete and Patch example. Maybe it will help someone