我正在访问不同的数据服务器,我在不同的类中尝试了不同的方法,使用基本的http :: net,curb,rest-client和open-uri
(1)如何测量Ruby/Rails中的性能?(2)您认为哪种方法更快?
所有4种不同方法的示例代码:
url = "..."
begin
io_output = open(url, :http_basic_authentication => [@user_id, @user_password])
rescue => e
error = e.message #for debugging return this
return '-'
else
output = io_output.read
Run Code Online (Sandbox Code Playgroud)
要么
require 'net/https'
uri = URI.parse("...")
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_PEER
data = http.get(uri.request_uri) #http request status
res = Net::HTTP.get_response(uri)
puts res.body if res.is_a?(Net::HTTPSuccess)
Run Code Online (Sandbox Code Playgroud)
要么
require 'curb'
url = "..."
c = Curl::Easy.new(url) do |curl|
curl.headers["Content-type"] = "application/json"
curl.headers["Authorization"] = "Token ..."
end …Run Code Online (Sandbox Code Playgroud) 我一直在使用RestClient请求:
response = RestClient.post server_url, post_params, accept: :json
Run Code Online (Sandbox Code Playgroud)
哪个一直很好.但我需要增加超时,因为当服务器执行上传时,它不会偶尔完成.
我研究过并发现唯一的解决方案是将语法更改为:
response = RestClient::Request.execute(:method => :post, :url => server_url, post_params, :timeout => 9000000000)
Run Code Online (Sandbox Code Playgroud)
但是,我似乎无法传递参数('post_params')的hashmap,就像我能够在之前的调用中一样.我应该如何编写请求以便'post_params'包含在内.这是一个复杂的hashmap,所以我无法扩充或摆脱它.
非常感谢帮助.
我一直在尝试在我的CodeIgniter RestClient控制器中发出POST请求以在我的RestServer中插入数据,但看起来我的POST请求是错误的.
这是我在控制器中的RestClient POST请求:
$method = 'post';
$params = array('patient_id' => '1',
'department_name' => 'a',
'patient_type' => 'b');
$uri = 'patient/visit';
$this->rest->format('application/json');
$result = $this->rest->{$method}($uri, $params);
Run Code Online (Sandbox Code Playgroud)
这是我的RestServer的控制器:耐心
function visit_post()
{
$insertdata=array('patient_id' => $this->post('patient_id'),
'department_name' => $this->post('department_name'),
'patient_type' => $this->post('patient_type') );
$result = $this->user_model->insertVisit($insertdata);
if($result === FALSE)
{
$this->response(array('status' => 'failed'));
}
else
{
$this->response(array('status' => 'success'));
}
}
Run Code Online (Sandbox Code Playgroud)
这是user_model
public function insertVisit($insertdata)
{
$this->db->insert('visit',$insertdata);
}
Run Code Online (Sandbox Code Playgroud) 我需要创建一个与REST服务器对话的应用程序.
我发现答案:Android REST客户端,Sample? 但它是2012年.
是否有一个我可以遵循的教程(以及您建议的)以获得一个小的工作示例项目?提前致谢.
我正在尝试使用RestClient Ruby gem模仿curl请求,到目前为止,我在尝试发送有效负载方面遇到了很多麻烦.我的卷曲请求看起来像这样
curl URL -X POST -u API_KEY -d '{"param_1": "1"}'
我一直试图用RestClient复制这个,使用类似的东西:
RestClient::Request.execute(method: :post, url: URL, user: API_KEY, payload: {"param_1" => "1"})
唉,这样做时我一直收到400 - Bad Requests错误.我是以错误的方式发送数据吗?我应该使用除有效载荷之外的东西吗?
我正在学习Swagger以及如何使用Swagger代码生成REST客户端。我知道如何使用Swagger进行文档编制,也知道如何使用Swagger生成简单的REST Server,但是我不知道如何使用Swagger代码生成简单的REST Client。
例如,我有一个简单的应用程序,它是一个REST Server,并且我想生成REST Client。我可以使用Swagger代码生成代码吗?
REST服务器的控制器:
package com.dgs.spring.springbootswagger.controller;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
@RestController
@RequestMapping("/api/v1")
@Api(value = "Employee Management System", description = "Operations pertaining to employee in Employee Management System")
public class EmployeeController {
@Autowired
private EmployeeRepository employeeRepository;
@ApiOperation(value = "View a list of available employees", response = List.class)
@ApiResponses(value = {
@ApiResponse(code = 200, message = "Successfully retrieved list"),
@ApiResponse(code = 401, message = "You are not authorized to view the resource"),
@ApiResponse(code …Run Code Online (Sandbox Code Playgroud) VSCode Rest Client 的文档缺乏很好的解释。这是他们举的例子。
POST https://api.example.com/user/upload
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW
------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="text"
title
------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="image"; filename="1.png"
Content-Type: image/png
< ./1.png
------WebKitFormBoundary7MA4YWxkTrZu0gW--
Run Code Online (Sandbox Code Playgroud)
不知道<是为了什么,也不知道是什么title?
我想调试我的Rails应用程序使用RestClient进行的请求.RestClient文档说:
要启用日志记录,您可以
使用ruby Logger设置RestClient.log或设置环境变量以避免修改代码(在这种情况下,您可以使用文件名"stdout"或"stderr"):
$ RESTCLIENT_LOG = stdout path/to/my/program生成这样的日志:
RestClient.get" http:// some/resource "
=> 200 OK | text/html 250个字节
RestClient.put" http:// some/resource ","payload"
=> 401未经授权| application/xml 340字节
请注意,这些日志是有效的Ruby,因此您可以将它们粘贴到restclient shell或>脚本中以重放您的休息调用序列.
如何将这些日志包含在我的Rails应用程序日志文件夹中?
使用RestClient gem,我需要创建一个如下所示的请求:
GET http://host/path?p=1&p=2
Run Code Online (Sandbox Code Playgroud)
完成此任务的正确语法是什么?请注意,接收主机不是Rails.
尝试:
resource = RestClient::Resource.new( 'http://host/path' )
params = { p: '1', p: '2' }
# ^ Overrides param to have value of 2 (?p=2)
params = { p: ['1','2'] }
# ^ results in 'p[]=abc&p[]=cde' (array [] indicators not wanted)
resource.get( { params: params } )
Run Code Online (Sandbox Code Playgroud) 我正在开发使用 microprofile Rest 客户端的应用程序。该 REST 客户端应发送带有各种 http 标头的 REST 请求。某些标头名称会动态更改。我的微配置文件休息客户端应该是通用的,但我没有找到如何实现这种行为。根据文档,您需要通过注释指定实现中的所有标头名称,但这不是通用的。有什么方法可以“破解”它并以编程方式添加 HTTP 标头吗?
提前致谢
GenericRestClient genericRestClient = null;
Map<String, Object> appConfig = context.appConfigs();
String baseUrl = (String) appConfig.get("restClient.baseUrl");
path = (String) appConfig.get("restClient.path");
try {
genericRestClient = RestClientBuilder.newBuilder()
.baseUri(new URI(baseUrl)).build(GenericRestClient.class);
}catch(URISyntaxException e){
logger.error("",e);
throw e;
}
Response response = genericRestClient.sendMessage(path, value);
logger.info("Status: "+response.getStatus());
logger.info("Response body: "+response.getEntity().toString());
Run Code Online (Sandbox Code Playgroud)
通用休息客户端代码:
@RegisterRestClient
public interface GenericRestClient {
@POST
@Path("{path}")
@Produces("application/json")
@Consumes("application/json")
public Response sendMessage(<here should go any map of custom headers>, @PathParam("path") String pathParam, String jsonBody);
}
Run Code Online (Sandbox Code Playgroud) rest-client ×10
curl ×2
java ×2
rest ×2
ruby ×2
android ×1
codeigniter ×1
curb ×1
dart ×1
json ×1
logging ×1
microprofile ×1
open-uri ×1
php ×1
spring-boot ×1
swagger ×1