在Ruby RestClient gem中,超时和开放超时功能之间的区别是什么?
我也没有从gem的doc文件中得到任何东西.
我试图用rest-client宝石阅读400响应的主体.问题是rest-client通过将它作为一个错误来回应400,所以我无法找出任何方法来获取正文.
这是激励性的例子.考虑这个对facebook图API的调用:
JSON.parse(RestClient.get("https://graph.facebook.com/me?fields=id,email,first_name,last_name&access_token=#{access_token}"))
Run Code Online (Sandbox Code Playgroud)
如果access_token过期或无效,Facebook会做两件事:
{
"error": {
"message": "The access token could not be decrypted",
"type": "OAuthException",
"code": 190
}
}
Run Code Online (Sandbox Code Playgroud)
因为400响应引发了错误,我无法弄清楚如何获得响应的主体.也就是说,例如,如果我在curl或浏览器中运行上面的GET请求,我可以看到正文,但我无法弄清楚如何在restclient中访问它.这是一个例子:
begin
fb_response = JSON.parse(RestClient.get("https://graph.facebook.com/me?fields=id,email,first_name,last_name&access_token=#{access_token}"))
rescue => e
# 400 response puts me here
# How can I get the body of the above response now, so I can get details on the error?
# eg, was it an expired token? A malformed token? Something else?
end
Run Code Online (Sandbox Code Playgroud) 我目前正在使用RestClient,似乎无法弄清楚如何输出请求xml和响应xml用于调试和信息目的...
我尝试了这里提到的解决方案:http: //agileice.blogspot.com/2009/09/pretty-printing-xml-results-returned.html
但这没有奏效,还有其他任何建议吗?
两年过去了,有片段,意图服务,游标加载器.方法是否仍然是最新的,或者是否有任何更好或成熟的模式来设计Android休息客户端,特别是与选项B相比(我没有权限发布图像,而是可以从这篇文章中找到图像).
我知道内容提供商部分是必不可少的.那么服务助手和服务组件呢?到目前为止,startService方法是Context或其子类的本质.这意味着服务助手将是一项活动.因此,从内容提供商发起活动是优雅的,还是应该从顶部的活动开始.
有服务,意图服务,asyncTask和线程.在我看来,意图服务适合于从远程服务器同步大量数据.这就是为什么他们在iosched中使用它.但常见的情况是只有部分项目将与远程服务器同步.意图服务太沉重了.甚至是服务方式.我们可以只使用内容提供程序中的asyncTask或线程或其中的某个组件来完成此类任务.或者是否有任何令人信服的理由使用该服务,并通过服务助手服务处理器路径.我说的是一个严肃的申请.
你有什么看法?
我正在使用春天RestTemplate来消耗休息服务(在春季休息时暴露).我能够消耗成功的场景.但对于负面情况,服务会返回错误消息和错误代码.我需要在我的网页中显示这些错误消息.
例如,对于无效请求,服务会抛出HttpStatus.BAD_REQUEST适当的消息.如果我把try-catch块转到catch块而我无法得到ResponseEntity对象.
try {
ResponseEntity<ResponseWrapper<MyEntity>> responseEntity = restTemplate.exchange(requestUrl, HttpMethod.POST, entity,
new ParameterizedTypeReference<ResponseWrapper<MyEntity>>() {
});
responseEntity.getStatusCode();
} catch (Exception e) {
//TODO How to get response here, so that i can get error messages?
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
如何获得ResponseWrapper例外情况?
我从这里开始阅读CustomRestTemplate,但无法确定哪一个最适合我的情况.ResponseExtractor
我一直在使用rest-client访问我编写的rails应用程序.我写了一个快速脚本来登录并发布帖子请求.一切正常,但我必须解决这样一个事实,即如果你在json中请求表单,则不会提供authenticity_token.我不得不在其他地方做一个普通的html请求获取authenticity_token,然后将其包含在我作为帖子请求的一部分提交的json中.基本上我有一个快速的脏脚本,如下所示
private_resource = RestClient::Resource.new( 'https://mysite.com')
params = {:user => {:email => 'user@mysite.com', :password => 'please'}}
#log in
login_response = private_resource['users/sign_in'].post(params, :content_type => :json, :accept => :json)
#get cookie
cookie = login_response.cookies
#get json
json_response = private_resource['products/new'].get(:content_type => :json, :accept => :json, :cookies => cookie)
#another request that returns html form with authenticity token
response_with_token = private_resource['products/new'].get( :cookies => cookie)
#extract token
token = Nokogiri::XML(response_with_token).css('input[name=authenticity_token]').first.attr('value')
#update cookie
cookie = response_with_token.cookies
#populate form and insert token
form = JSON.parse(json_response)
form['name'] = "my …Run Code Online (Sandbox Code Playgroud) 我正在使用JSON对象使用Spring MVC.当我想从RESTClient发送JSON对象时,我得到了
HTTP状态400 - 客户端发送的请求语法错误().
这是我的控制器
ObjectMapper mapper=new ObjectMapper();
@RequestMapping(value = "/addTask", method = RequestMethod.GET)
public ModelAndView addTask(@RequestParam("json") String json) throws JsonParseException, JsonMappingException, IOException
{
System.out.println("Json object from REST : "+json);
Task task=(Task) mapper.readValue(json, Task);
service.addService(task);
return new ModelAndView("Result");
}
Run Code Online (Sandbox Code Playgroud)
我的请求网址: http://localhost:8080/Prime/addTask
我的Json对象:
{"taskName":"nothing","taskId":1234,"taskDesc":"什么都没做"}
我也尝试在RESTClient中指定"Content-Type:application/json"但仍然得到相同的错误
我正在访问不同的数据服务器,我在不同的类中尝试了不同的方法,使用基本的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) 我正在尝试将 Insomnia 与客户端证书一起使用。我从 Insomnia 文档中遵循了此文档。我添加了我的证书 pem 文件和密码。
问题是我仍然收到此错误:
错误:无法连接到服务器。
你知道为什么吗?谢谢。
我试图为我的休息客户端设置标题,但每次我必须写
webclient.get().uri("blah-blah")
.header("key1", "value1")
.header("key2", "value2")...
Run Code Online (Sandbox Code Playgroud)
如何使用 headers() 方法同时设置所有标题?
rest-client resttemplate java-8 spring-boot spring-webclient