- Home /
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:
Is there a way to make this method allowed? If not, is there a work-around?
Is there really no way to send byte[] data through UWR.Post()?
Please help my solve this in any other way. This is crucial to me.
Answer by Bunny83 · Jan 18, 2021 at 09:27 AM
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.
$$anonymous$$aybe I'm doing something wrong?
Well, what are you doing on your serverside? HTTP in general is a read only, request based protocol. It's not a file system. The new verbs PUT and $$anonymous$$ETE are kind of an extension to give almost file system like access to resources. However the handling of such requests is generally controlled / consumed by server side scripts which perform the necessary actions on the server. You usually would have a server side script like a php script which you actually contact through your URL. That script would take care about either returning the requested data or update the data on the server.
You usually do not want to allow users to directly modify files on your server. On the wiki there's a server side highscore example where it stores the highscores in a mysql database. It comes with two serverside php scripts. One to add a new score and one to retrieve the current highscore list.
It's not really clear what you want to do. However you really have to be careful when you update or write files directly on the server. Users could upload malicious serverside script to your server if you're not careful. You really should read up on server security before you hack something together ^^.
I understand what you're saying, truly. But in my case it's not the users who gonna upload this file. It's a binary file that stores a record of all the people that played the game and is visible only for me in an ad$$anonymous$$ panel inside the game. Each time a player is finished playing, the game itself should upload a new file to override the old file, and to keep it updated.
Also, I have a lot of information about each player, about 10 properties if I recall correctly.
Your answer
![](https://koobas.hobune.stream/wayback/20220613023512im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
WebGL Uncaught RangeError 2 Answers
Send a form to a HTTP server in rails 0 Answers
How do I get into my StreamingAssets folder for the Web? 0 Answers