68

We have this software that has a webservices component.

Now, the administrator of this system has come to me, wanting to import data into the system by using the webservices component.

So, I went to read the documentation to try to figure this thing out and I am seeing things like this:


Click here to see what I'm talking about (this looks best in firefox, chrome, & safari)

That documentation gives examples of interacting with the system using HTTP verbs such as GET, POST, PUT, DELETE. But in my limited experience, I have never had to send neither an HTTP PUT nor a DELETE.

How do you do it? I have built HTML forms that have method="post" or method="get" and the request is sent to whatever is specified in the action attribute (action="someResource"). But I don't really know what to do with this PUT thing.

If I had to guess, I would have to build an application that creates some sort of an HTTP Request object and set all the properties of it and somehow include the data I want to PUT to the RESOURCE (


I am trying to use REST terminology, which is something else is very new to me
). Then I would send the request using my programming language and blah blah blah. I am just speculating on this. Please offer up some assistance!

I thought that I was a web developer, since I know things like XHTML, CSS, JavaScript, etc. but it's starting to look like I don't know anything about the foundations of the web at all (HTTP).

EDIT

PS: I program mostly with .net. So, any examples in .net would be pretty awesome.

Community
  • 1
  • 1
Ronnie Overby
  • 45,287
  • 73
  • 267
  • 346
  • PUT and DELETE are mostly obsolete these days. PUT has been replaced by POST forms with the multipart/form-data encoding and file input controls. DELETE has been largely replaced by POST to a script with arguments telling it which files to delete. – Powerlord May 04 '09 at 19:20
  • 6
    I don't think those methods are obsolete so much as they are ignored. I believe that POST is often used to do things that it wasn't really intended for. RESTful development practices seek to use HTTP in the way it was designed to be used. Am I right on this? REST is a new concept for me. – Ronnie Overby May 05 '09 at 14:07
  • 37
    Let me state this a bit more strongly. PUT and DELETE are definitely not obsolete. Browsers do make it difficult to do PUT and DELETE but you can always use javascript and the XmlHttpRequest object. – Darrel Miller May 07 '09 at 16:01
  • 5
    PUT and DELETE are used in for example WebDav, certainly not obsolete. – PQW Jun 09 '09 at 18:28
  • 9
    PUT and DELTE are fundamental to a truly RESTful API – Nick Katsivelos Dec 03 '09 at 16:08
  • 2
    @R. Bemrose: You have >23k reputation and you say PUT/DELETE are obsolete? What in the world makes you say that? I could say they were not really used for some time on www, but they are getting more and more these days again (due to XHR). – Robert Koritnik Feb 10 '11 at 12:27
  • Ahhh the good old days when I was an HTTP newb. – Ronnie Overby Dec 11 '13 at 19:37

11 Answers11

23

Here's a C# example using HttpWebRequest:

using System;
using System.IO;
using System.Net;

class Test
{
        static void Main()
        {
                string xml = "<xml>...</xml>";
                byte[] arr = System.Text.Encoding.UTF8.GetBytes(xml);
                HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("http://localhost/");
                request.Method = "PUT";
                request.ContentType = "text/xml";
                request.ContentLength = arr.Length;
                Stream dataStream = request.GetRequestStream();
                dataStream.Write(arr, 0, arr.Length);
                dataStream.Close();
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                string returnString = response.StatusCode.ToString();
                Console.WriteLine(returnString);
        }
}

Update: there's now an HttpClient class in System.Net.Http (available as a NuGet package) that makes this a bit easier:

using System;
using System.Net.Http;

class Program
{
    static void Main()
    {
        var client = new HttpClient();
        var content = new StringContent("<xml>...</xml>");
        var response = client.PutAsync("http://localhost/", content).Result;
        Console.WriteLine(response.StatusCode);
    }
}
Jason DeFontes
  • 2,235
  • 15
  • 14
  • This looks very promising. I will try this out. Thanks – Ronnie Overby May 02 '09 at 04:44
  • HttpWebRequest doesn't support verbs other than GET or POST. Setting Method to "PUT" will throw a NotSupportedException (sucks, I know!) - http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.method(VS.96).aspx – user141682 Feb 10 '10 at 06:56
  • 8
    @Luke It's only on Silverlight that PUT is not supported (your link is to the Silverlight docs). The code above runs as written otherwise. – Jason DeFontes Feb 11 '10 at 22:36
  • 1
    thanks! I didn't realise this. I've since found the HttpClient class from the WCF Rest Toolkit which is fantastic. – user141682 Feb 18 '10 at 14:18
  • It's 2018, this is still relevant because you made is so easy + clear. What I was thinking is why we use client.PutAsync() instead of PutAsXmlAsync() – Mese Mar 23 '18 at 11:41
15

PUT and DELETE are likely to require that you use AJAX and make XMLHttpRequests since the FORM tag only supports GET and POST verbs and links only make GET requests.

With jQuery:

 $.ajax( {
       url: '/controller/action',
       type: 'PUT',
       data: function() { ...package some data as XML },
       dataType: 'xml',
       ... more options...
 );

The note on the jQuery ajax options page warns that some browsers don't support PUT and DELETE for the request type. FWIW, I've never used PUT but have used DELETE in IE and FF. Haven't tested in Safari or Opera.

tvanfosson
  • 524,688
  • 99
  • 697
  • 795
  • I will likely not write the code to solve this problem for use on a web server/web browser. Likely, I will write a console/windows app that reads data from a database/spreadsheet, then creates some xml to PUT/POST/FART or whatever back to the server in an HTTP request. The purpose of my question here is just to get some information on executing these unfamiliar verbs. HAHAHAHA – Ronnie Overby May 01 '09 at 19:24
  • You could always just have a hidden field that has a hidden "method" field when accessing your rest api through an html form – brndnmg Nov 23 '09 at 18:06
8

Here is how to do it in CURL: How to Use cURL to Test RESTful Rails

Or...you can definitely use an HTML form. If the app is truly RESTful, it will understand the REST actions and only let you perform certain actions based on the method you use.

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
Tony
  • 18,776
  • 31
  • 129
  • 193
  • If using an HTML form (which I would like to try just for education/proof of concept) how do I send the XML back to the server? – Ronnie Overby May 01 '09 at 19:04
  • Get curl for windows here: http://curl.haxx.se/download.html (don't mind the intimidating haxx in the url!) – cgp May 01 '09 at 19:05
  • Look at the comment by tvanfosson. It shows a good way to make a PUT request. And then XML should be returned...? – Tony May 01 '09 at 19:55
6

You can't PUT using an HTML form (the spec defines only GET/POST for forms).

However any HTTP API should allow you to PUT, in the same way that it allows you to GET or POST. For example, here's the Java HTTPClient documentation, which details PUT alongside all the other HTTP verbs.

I don't know which language you're using, but I think it's going to be pretty trivial to write an app to perform an HTTP PUT.

Brian Agnew
  • 268,207
  • 37
  • 334
  • 440
6

I found this really cool piece of free software called RESTClient.

It lets you interact with HTTP resources using various verbs, manually setting headers and the body, setting authentication info, ssl, running test scripts, etc.

This will help me to figure out how to interact with our "webservices" software which is really just a RESTful API to the software's database.

Ronnie Overby
  • 45,287
  • 73
  • 267
  • 346
2

Test the api as a chrome extension https://chrome.google.com/webstore/detail/fdmmgilgnpjigdojojpjoooidkmcomcm

everconfusedGuy
  • 2,709
  • 27
  • 43
1

How about giving libcurl.NET a try: http://sourceforge.net/projects/libcurl-net/

user141682
  • 432
  • 6
  • 9
1

Here is a tool that lets you drag and drop to PUT files

Yishai
  • 90,445
  • 31
  • 189
  • 263
1

"Now, the administrator of this system has come to me, wanting to import data into the system by using the webservices component."

Web services have little to do with HTML forms.

Web services requests are either done from Javascript (e.g., as Ajax) or they're done from your application programs.

You would write a C# or VB program that used HTTP to do a Put to the given web services URL with the given set of data.

Here, for instance, is some sample VB code: http://developer.yahoo.com/dotnet/howto-rest_vb.html#post

Replace the method string of "POST" with "PUT".

S.Lott
  • 384,516
  • 81
  • 508
  • 779
0

PUT and DELETE are not part of HTML4, but are included in the HTML5 specifications. For this reason, most popular browsers don't have good support for them, since they focus on HTML4. However, they are definitely part of HTTP and always have been. You do a PUT using some non-browser client, or using a form in an HTML5-ready browser.

Update: PUT and DELETE are no longer part of HTML5 for forms. See: http://www.w3.org/TR/html5/association-of-controls-and-forms.html#attr-fs-method

aehlke
  • 15,225
  • 5
  • 36
  • 45
0

Just a headsup some network admins block puts for various reasons. So you may have to use a POST instead of PUT. Check with your operations.

Srikar Doddi
  • 15,499
  • 15
  • 65
  • 106