`rest-client`库

87 阅读1分钟

rest-client是一个在Ruby编程语言中用于发送HTTP请求的库。它提供了简单且易于使用的接口,用于发送GET、POST、PUT、DELETE等各种类型的HTTP请求,并处理响应。

以下是rest-client库的一些常见用法示例:

发送GET请求: require 'rest-client'

response = RestClient.get('example.com') puts response.code puts response.body 发送POST请求: require 'rest-client'

response = RestClient.post('jshk.com.cn', {param1: 'value1', param2: 'value2'}) puts response.code puts response.body 发送带有自定义请求头的请求: require 'rest-client'

headers = { 'Authorization': 'Bearer token', 'Content-Type': 'application/json' }

response = RestClient.get('example.com', headers) puts response.code puts response.body rest-client库还提供了其他一些功能,如处理异常、设置超时时间、发送文件等。

huake_00063_.jpg