5

I have tried to implement a REST WCF in order to explore difference between PUT and POST verb. I have uploded a file in a location using the service.

The service implementation is as folowing:

[OperationContract]
[WebInvoke(UriTemplate = "/UploadFile", Method = "POST")]
void UploadFile(Stream fileContents);

public void UploadFile(Stream fileContents)
{
 byte[] buffer = new byte[32768];
 MemoryStream ms = new MemoryStream();
 int bytesRead, totalBytesRead = 0;
 do
 {
       bytesRead = fileContents.Read(buffer, 0, buffer.Length);
       totalBytesRead += bytesRead;

       ms.Write(buffer, 0, bytesRead);
  } while (bytesRead > 0);

  using (FileStream fs = File.OpenWrite(@"C:\temp\test.txt")) 
  { 
      ms.WriteTo(fs); 
   }

  ms.Close();

}

Client code is as following:

HttpWebRequest request =     (HttpWebRequest)HttpWebRequest.Create("http://localhost:1922   /EMPRESTService.svc/UploadFile");
        request.Method = "POST";
        request.ContentType = "text/plain";

        byte[] fileToSend = File.ReadAllBytes(@"C:\TEMP\log.txt");  // txtFileName contains the name of the file to upload. 
        request.ContentLength = fileToSend.Length;

        using (Stream requestStream = request.GetRequestStream())
        {
            // Send the file as body request. 
            requestStream.Write(fileToSend, 0, fileToSend.Length);
            //requestStream.Close();
        }

        using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
            Console.WriteLine("HTTP/{0} {1} {2}", response.ProtocolVersion, (int)response.StatusCode, response.StatusDescription);
        Console.ReadLine();

The file is being uploaded and the response status code is being returned as "200 OK". The satus code is same in case of existance or non-existance of the file in the upload location.

I have changed the REST verb to PUT and the status code is same as above.

Could anybody explain, how I can identify the differences between the verbs in this context? I couldn't able to simulate generating continious request fron client code. If the behaviour will differ on doing so, could anybody help me in modifying the client code in ordrr to send continious request in a row ?

Tomasz Nurkiewicz
  • 334,321
  • 69
  • 703
  • 674
techmad
  • 933
  • 2
  • 11
  • 14

1 Answers1

2

POST verb is used when are you creating a new resource (a file in your case) and repeated operations would create multiple resources on the server. This verb would make sense if uploading a file with the same name multiple times creates multiple files on the server.

PUT verb is used when you are updating an existing resource or creating a new resource with a predefined id. Multiple operations would recreate or update the same resource on the server. This verb would make sense if uploading a file with the same name for the second, third... time would overwrite the previously uploaded file.

Dmitry S.
  • 8,373
  • 2
  • 39
  • 49
  • Thats true as far as theoritical concept is concerned. But in my case no multiple copy of the uploaded file has been generated, even after running the client program multiple times with POST verb in use.Running the client program mutiple times resulting in same behaviour for PUT and POST verbs in context of the above example.Could anybody suggest, what kind of modification is required in the example, in order to replicate the difference between the verbs? – techmad May 13 '12 at 18:24
  • Why do you expect a difference in the results when using PUT vs POST? – Dmitry S. May 13 '12 at 18:41
  • Then how I can understand, PUT and POST verbs works in different way? I want to prove the different behaviour of these two REST verbs using WCF REST or some other .NET Program. If my example cannot prove the same, could you please provide me an implemented example? – techmad May 14 '12 at 09:34
  • I think the main thing to remember is that what the different verbs do is just a convention. This means code you write can do anything it wants with any verb, but people accessing it will be surprised if the behaviour differs from the REST specification. – Robert Elm May 14 '12 at 12:21
  • Like rodimius stated, POST vs PUT verbs are not going to behave differently. There are conventions about resources that I mentioned in my response by they should be enforced through the code. You need to set proper response codes for the scenarios from your code. – Dmitry S. May 14 '12 at 14:01
  • Well, in my case since there is WCF REST service handling the PUT or POST, who will take the responsiblity to send the relevant status code and desired behaviour? 1) IIS web server where the has been hosted ? 2) WCF REST Frame work ? 3) Manual implementation of code in the Service method ? – techmad May 14 '12 at 16:43
  • 1
    The developer is responsible. Here is how you can do it. http://stackoverflow.com/questions/140104/how-can-i-return-a-custom-http-status-code-from-a-wcf-rest-method – Dmitry S. May 14 '12 at 19:47