- Home /
WWW 'PUT' method?
I'm in need of using the PUT verb on a WWW request. I understand that WWW supports GET and POST, but does anyone know if it's possible (perhaps undocumented) to do a PUT? Or if there's a plugin/extension that can? Or other best way to do it?
Answer by Fehr · Jun 22, 2015 at 06:17 AM
Hey guys,
I initially wrote my client messaging layer around WebRequest, which has full support for CRUD but found that eventually with bigger data it caused unity to stutter, even when threaded.
When moving back to Unity's WWW I came across the lack of PUT and DELETE which my client was now coupled with. To avoid spending the time required to re-write all the PUT and DELETE MVC server functions, I came up with the following solution:
First, add the following header to your WWW request:
var headers = new Dictionary<string, string>();
headers.Add("X-HTTP-Method-Override", "PUT");
Then perform the WWW POST request as normal:
var request = new UnityEngine.WWW(_uri, _putBytes, headers);
yield return request;
Debug.Log(request.text);
The server receives the sent POST request, but we area able to intercept the message by adding a new method handler which is run over the message before it is passed to the server's functions, and we change the request's method to PUT instead of POST:
public class MethodOverrideHandler : DelegatingHandler
{
readonly string[] _methods = { "DELETE", "HEAD", "PUT" };
const string _header = "X-HTTP-Method-Override";
protected override Task<HttpResponseMessage> SendAsync(
HttpRequestMessage request, CancellationToken cancellationToken)
{
// Check for HTTP POST with the X-HTTP-Method-Override header.
if (request.Method == HttpMethod.Post && request.Headers.Contains(_header))
{
// Check if the header value is in our methods list.
var method = request.Headers.GetValues(_header).FirstOrDefault();
if (_methods.Contains(method, StringComparer.InvariantCultureIgnoreCase))
{
// Change the request method.
request.Method = new HttpMethod(method);
}
}
return base.SendAsync(request, cancellationToken);
}
}
Add the following line to the server solution's WebApiConfig.cs:
public static void Register(HttpConfiguration config)
{
config.MessageHandlers.Add(new MethodOverrideHandler());
...
This way, without re-factoring any of the business layer for either the client or server we can overcome the lack of PUT and DELETE in Unity's WWW class. If anyone wants any more info on the process, there's a great article by Scott Hanselman that provided me with the solution.
Answer by Bunny83 · Apr 27, 2011 at 02:04 AM
AFAIK WWW doesn't support any other method except GET and POST, even HEAD is missing. Documented or not C# shows you all available functions and there's nothing to initiate a PUT. Can't you use a POSt instead? Most file uploads are done with forms and POST method. I'm not even sure if every webserver (especially the free webhoster) supports PUT. I never used PUT yet. If you really need to use it i guess you have to use a custom http connection via MONO's WebRequest.
Yeah, I'll look into that. Not my server, I have no control over it, and a proxy would be a real pain.
"cant you use a post ins$$anonymous$$d?". No, that's not an option for anyone working with a RESTful API.
Your answer
Follow this Question
Related Questions
PUT with WWWform 3 Answers
WWW Delete and Put 0 Answers
Why isn't PUT supported in WWW 1 Answer
Upload to AWS S3 from Unity 0 Answers
Unity 4.0.0f7 GetThreadContext failed 2 Answers