Rest client–simple DSL for accessing HTTP and REST resources

Source: Internet
Author: User
Tags http post

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







Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.