Error: HTTP/1.1 405 Method Not Allowed

When I try to use UnityWebRequest.Put() I get the error aforementioned.


I am hosting my game on HostGator.

I’ve asked their tech support but they have no idea and they say I should consult with a Web Developer.

So instead I’m consulting with this amazing community.


I want to state that my initial plan was to use UnityWebRequest.Post(), but I can’t find a way to send byte through this function. It is only possible to do so with the UnityWebRequest.Put() function.


This is the part of my code where I try to upload a new binary file:

public static PlayerData[] OldPlayersData;

    public static IEnumerator SavePlayer(PlayerData player)
    {
        byte[] finalDataToSend;

        PlayerData[] newData = new PlayerData[] { player };

        newData = newData.Concat(OldPlayersData);

        BinaryFormatter bf = new BinaryFormatter();

        using (MemoryStream ms = new MemoryStream())
        {
            bf.Serialize(ms, newData);
            finalDataToSend = ms.ToArray();
        }

        UnityWebRequest www = UnityWebRequest.Put(putPlayersURL, finalDataToSend);
        yield return www.SendWebRequest();

        if (www.isNetworkError || www.isHttpError)
        {
            Debug.LogError(www.error);
        }
        else
        {
            Debug.Log("done");
        }

    }

Bottom line, my questions are:

  1. Is there a way to make this method allowed? If not, is there a work-around?

  2. Is there really no way to send byte data through UWR.Post()?

  3. Please help my solve this in any other way. This is crucial to me.

1 Answer

1

This purely depends on the server you’re communicating with. The 405 reponse code is generated by the server. It tells you that the used method is not allowed for the resource in your URL. This is usually the result of not allowing any of the extra HTTP verbs like PUT or DELETE. See this SO question. If the support of your hoster doesn’t know what you’re talking about, they should change business -.- Though as you can read in the answer to the SO question, free / shared hoster usually don’t have those methods enabled.

So your options are:

  • ask your current hoster if they can enable the extra verbs like PUT / DELETE / (whatever you may also need)
  • change to a hoster that specifically supports the use of those request methods.
  • change your API (if possible) so that it uses a supported verb. Usually POST should work on all free hoster.
  • finally you could host your own server where you have full control over the server and its settings.

There are not really alternatives. I don’t quite understand your comment that POST does not support sending data. Of course it does. Though you have to use an UploadHandlerRaw because Unity did not provide an overload of the Post method to initiate a post request with a byte array. Something like this should work

UnityWebRequest www = new UnityWebRequest( YourURL , UnityWebRequest.kHttpVerbPOST );
UploadHandlerRaw MyUploadHandler = new UploadHandlerRaw( finalDataToSend );
MyUploadHandler.contentType= "application/octet-stream";
www.uploadHandler= MyUploadHandler;
yield return www.SendWebRequest();

Finally I should add that you should not use the BinaryFormatter, especially when saving / loading data over the internet. The BinaryFormatter is not secure. So using it will make your application vulnerable to all sorts of security issues. Please read the security guide that Microsoft has released. You should use more robust formats like json. If you really want a binary format you could either compress the json string or roll your own binary format using BinaryWriter / BinaryReader.

First of all let me say that you've made me a bit smarter with your respond. Now, I tried the piece of code that you suggested, and although it does seem to work (there is no error to be printed), it is actually not working, because when I get the data again, it is the same.. It did not change. So I don't know what it did with what I sent, but it surely didn't replaced my file. Maybe I'm doing something wrong?

–