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 (). 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 am trying to use REST terminology, which is something else is very new to me
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).
PS: I program mostly with .net. So, any examples in .net would be pretty awesome.
Sending a PUT Request with Axios The simplest way to make the PUT call is to simply use the put() function of the axios instance, and supply the body of that request in the form of a JavaScript object: const res = await axios. put('/api/article/123', { title: 'Making PUT Requests with Axios', status: 'published' });
The PUT method requests that the enclosed entity be stored under the supplied Request-URI. If the Request-URI refers to an already existing resource, the enclosed entity SHOULD be considered as a modified version of the one residing on the origin server.
The HTTP PUT request method creates a new resource or replaces a representation of the target resource with the request payload.
So yes, a PUT request, technically, strictly, has to have a body.
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); } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With