Https://github.com/rest-client/rest-client
Https://github.com/jnunemaker/httparty
http://ruby-doc.org/stdlib-2.1.2/libdoc/net/http/rdoc/Net/HTTP.htmlhttps://www.imququ.com/post/ Four-ways-to-post-data-in-http.html
Work is often used to get an XML or JSON-formatted data to parse, get the valid data we want to get, this time with the HTTP GET request,
When we submit the form and upload the file, we use the HTTP POST request, the ContentType of the POST request is divided into two kinds,
When submitting a regular form, Content-type is "application/x-www-form-urlencoded",
When uploading a file, Content-type is "Multipart/form-data"
This can be viewed through the browser's debug tool
Click F12 to open the Page debugging tool and select "Network", "Headers", "Request Headers", "Content-type"
Let's take a look at the application scenario separately.
A REST Client gets Xml/json data
(1) XML
Response = restclient.get (URL)
Url_hash = Hash.from_xml (response.body)
(2) JSON
Response = Restclient.get (Url_list[index])
Url_hash = Json.parse (response.body) [' Headline_videos ']
(ii) Httpparty get JSON data
Response = Httparty.get (' Https://api.stackexchange.com/2.2/questions?site=stackoverflow ')
==============================
The biggest advantage of restclient is the ability to upload a file when a post is requested,
Module Classmethodsdefupload_image (file_path) timestamp=Time.now.to_i Response=Restclient.post Settings.upload.image.url, {uid:Settings.upload.image.uid, appkey:Settings.upload.im Age.appkey, Timestamp:timestamp, sign:sign (timestamp), FileName:"Ott_cms_#{timestamp}", Duplmd5:Settings.upload.image.duplmd5, File:File.new (File_path)} case Response.code when200parse_upload_response ResponseElse{errors: ["Image upload Service is temporarily unavailable"]} end end