How do you do an HTTP Put?

…衆ロ難τιáo~ 提交于 2019-11-27 17:40:08

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);
    }
}

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.

Tony

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.

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.

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.

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

"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".

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

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.

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!