How to make an HTTP request in a menu item ?

So what I’m trying to do is to have a menu command that will send a GET request to an API. The problem I’ve run into is that menu item requires the function to be static, while making an http request uses coroutines which requires the function to be non-static (from what I’ve understood and experienced). Here is my (non-working) code :

public class CreateMaterialExample : MonoBehaviour
{
    [MenuItem("GameObject/API Request")]
    static void APIRequestFromMenu()
    {
        StartCoroutine("GetRequest", "Parameter");
    }

    IEnumerator GetRequest(string parameter)
    {
        UnityWebRequest www = UnityWebRequest.Get("https://www.myurl.com/some/api?parameter="
            + System.Uri.EscapeUriString(parameter));

        yield return www.SendWebRequest();

        if (www.isNetworkError || www.isHttpError)
        {
            Debug.Log(www.error);
        }
        else
        {
            Debug.Log(www.downloadHandler.text);
        }
    }
}

The problem here is that Unity doesn’t want to hear about “StartCoroutine” since it is in a static method. If I remove static Unity complains about the menu item requiring the function to be static.

I would be happy to hear about a solution that doesn’t use coroutines, but in a larger sense I’m curious to know whether it could be possible to use coroutine in menu item functions. I get it if the current implementation makes it impossible, but I can’t see a reason why menu item couldn’t use asynchronous functions.

You can use this Open Source plugin to use promises instead of Coroutines GitHub - proyecto26/RestClient: 🦄 A Promise based REST and HTTP client for Unity 🎮

extracted from: Unity - Scripting API: WWW .

"You start a download in the background by calling WWW(url) which returns a new WWW object.

You can inspect the isDone property to see if the download has completed or yield the download object to automatically wait until it is (without blocking the rest of the game)."

So I tested something like this and its works so far:

public class wexample : MonoBehaviour
{

    static UnityWebRequestAsyncOperation request;
    [MenuItem("GameObject/API Request")]
    static void APIRequestFromMenu()
    {
        UnityWebRequest www = UnityWebRequest.Get("https://www.myurl.com/some/api?parameter="
            + System.Uri.EscapeUriString("parameter"));

        request = www.SendWebRequest();

        while(!request.isDone){
            print(request.progress);
        }

    }

}