标签: rest-client

我可以覆盖RESTClient默认的"HttpResponseException"响应> 399返回代码吗?

我正在使用Groovy RESTClient类为Java WebServices写一些(spock)验收测试我一直在创作.

我遇到的一个挫折就是测试答案......

200 状态很简单:

when:  def result = callServiceWithValidParams()
then:  result.status == 200
Run Code Online (Sandbox Code Playgroud)

但是400+我被迫要么包装,要么默认try-catch测试HttpResponseException那个RESTClient抛出.

when:
    callWithInvalidParams()
then:
    def e = thrown(Exception)
    e.message == 'Bad Request'
Run Code Online (Sandbox Code Playgroud)

这有点好,如果有点令人沮丧......但我想做得更好.

理想情况下,我希望我的测试更像这样(如果你不使用groovy/spock,可能会让人感到困惑)

@Unroll
def "should return #statusCode '#status' Response"()
{
    when:
    def result = restClient.get(path: PATH, query: [param: parameter])

    then:
    result.status == statusCode

    where:
    status         | statusCode | parameter                
    'OK'           | 200        | validParam
    'Bad Request'  | 400        | invalidParam
}
Run Code Online (Sandbox Code Playgroud)

在上面的示例中,"错误请求"案例失败. …

testing groovy rest-client spock

13
推荐指数
2
解决办法
2914
查看次数

适当的Android REST客户端

我为Android应用程序创建了自己的REST客户端库,但在观看了关于该主题的Google I/O演示文稿之后,我意识到我错了(正是他们显示幻灯片9).

现在我想以正确的方式再次做到这一点,但我想知道是否有一个可以省去麻烦的图书馆.我们在服务器端使用Jersey.

我看过不同的解决方案:CRestResty,但是我想找到一个Android解决方案,所以我不需要自己实现ContentProvider的东西,而且android-jbridge,但它看起来并不活跃.

此时我正在考虑使用RestTemplate(来自Spring Android)并自己编写它周围的东西,但这需要一些时间.

有更好的选择吗?

rest android jersey rest-client

12
推荐指数
1
解决办法
9115
查看次数

RestTemplate GET请求请求参数

我必须调用REST Web服务,我计划使用RestTemplate.我查看了如何发出GET请求的示例,如下所示.

 String result = restTemplate.getForObject("http://example.com/hotels/{hotel}/bookings/{booking}", String.class,"42","21");
Run Code Online (Sandbox Code Playgroud)

在我的例子中,RESTful url就像下面这样.在这种情况下如何使用RestTemplate?

http://example.com/hotels?state=NY&country=USA
Run Code Online (Sandbox Code Playgroud)

所以我的问题是如何发送GET请求的请求参数?

parameters get request rest-client resttemplate

12
推荐指数
1
解决办法
3万
查看次数

Ruby中的REST客户端示例

任何人都可以用一个例子来解释我,使用REST Client在Rest Web服务中进行GET/POST/PUT操作吗?

在POST/PUT中,使用REST Client,需要传递整个xml主体来进行POST/PUT操作.

例如,使用 REST Client

我需要使用,获取服务的内容,

      RESTClient.get(url)
Run Code Online (Sandbox Code Playgroud)

将xml发布到url:

      RESTClient.post(url,entirexml)
Run Code Online (Sandbox Code Playgroud)

将xml输入URL:

      RESTClient.put(url,entirexml)
Run Code Online (Sandbox Code Playgroud)

使用REST CLIENT进行删除.

任何人都可以通过示例帮助我提供所有REST客户端HTTP方法的示例吗?

我需要使用REST Client的PUT/POST操作将整个XML和命名空间一起发送到休息服务.

如果有人有这方面的例子,那么请发帖.

ruby rest rest-client

12
推荐指数
2
解决办法
5万
查看次数

如何使用Spring的REST模板传递自定义对象

我需要使用RESTTemplate将自定义对象传递给我的REST服务.

RestTemplate restTemplate = new RestTemplate();
MultiValueMap<String, Object> requestMap = new LinkedMultiValueMap<String, Object>();
...

requestMap.add("file1", new FileSystemResource(..);
requestMap.add("Content-Type","text/html");
requestMap.add("accept", "text/html");
requestMap.add("myobject",new CustomObject()); // This is not working
System.out.println("Before Posting Request........");
restTemplate.postForLocation(url, requestMap);//Posting the data.
System.out.println("Request has been executed........");
Run Code Online (Sandbox Code Playgroud)

我无法将自定义对象添加到MultiValueMap.请求生成失败.

有人可以帮我找到一个方法吗?我可以简单地传递一个字符串对象而没有问题.用户定义的对象会产生问题.

感谢任何帮助!

spring-mvc rest-client spring-3 resttemplate

12
推荐指数
1
解决办法
6万
查看次数

如何为Ruby的RestClient设置用户代理?

我对使用ruby RestClient gem时设置自己的用户代理感兴趣.

http://github.com/archiloque/rest-client

但是,我找不到任何有关如何操作的文档.有什么指针吗?

ruby user-agent rest-client

12
推荐指数
1
解决办法
5804
查看次数

RestClient只用最后一个哈希去除哈希参数数组?

我有一个条件,我需要传递一个参数作为哈希数组,如下所示:

以下是API调用的Rack :: Test post方法.

post "#{url}.json",
:api_key => application.key,
:data => [{"Company"=>"Apple,Inc","Website"=>"Apple.com"},{"Company"=>"Google","Website"=>"google.com"}],
:run => { :title => "The First Run" }
Run Code Online (Sandbox Code Playgroud)

这是rails应用程序的日志.

Parameters: {"api_key"=>"6a9acb84d0ea625be75e70a1e04d26360606ca5b", "data"=>[{"Company"=>"Apple,Inc", "Website"=>"Apple.com"}, {"Company"=>"Google", "Website"=>"google.com"}], "run"=>{"title"=>"The First Run"}, "line_id"=>"4e018e2c55112729bd00000a"}
Run Code Online (Sandbox Code Playgroud)

现在,这是我用来调用API的RestClient post方法.

RestClient.post("/lines/#{@line.id}/runs.json", {:run => {:title => @title}, @param_for_input => @param_data})
Run Code Online (Sandbox Code Playgroud)

这是rails应用程序的日志.

Parameters: {"run"=>{"title"=>"run name"}, "data"=>{"Company"=>"Google", "Website"=>"google.com"}, "api_key"=>"f488a62d0307e79ec4f1e6131fa220be47e83d44", "line_id"=>"4e018a505511271f82000144"}
Run Code Online (Sandbox Code Playgroud)

不同之处在于data参数.

使用Rack :: Test方法发送时,数据将作为 "data"=>[{"Company"=>"Apple,Inc", "Website"=>"Apple.com"}, {"Company"=>"Google", "Website"=>"google.com"}]

但是通过RestClient方式,参数数据数组被剥离出来,只传递最后一个哈希值 "data"=>{"Company"=>"Google", "Website"=>"google.com"}

为什么RestClient将哈希数组剥离到数组的最后一个哈希?

rubygems ruby-on-rails rest-client ruby-on-rails-3

11
推荐指数
2
解决办法
7750
查看次数

Ruby rest-client文件作为具有基本身份验证的多部分表单数据上载

我理解如何使用Ruby的rest-client使用基本身份验证来创建http请求

response = RestClient::Request.new(:method => :get, :url => @base_url + path, :user => @sid, :password => @token).execute
Run Code Online (Sandbox Code Playgroud)

以及如何将文件作为多部分表单数据发布

RestClient.post '/data', :myfile => File.new("/path/to/image.jpg", 'rb')
Run Code Online (Sandbox Code Playgroud)

但我似乎无法弄清楚如何将两者结合起来将文件发布到需要基本身份验证的服务器.有谁知道创建此请求的最佳方法是什么?

ruby post http basic-authentication rest-client

11
推荐指数
1
解决办法
2万
查看次数

在RSpec中记录RestClient响应

我有以下规格......

  describe "successful POST on /user/create" do
    it "should redirect to dashboard" do
      post '/user/create', {
          :name => "dave",
          :email => "dave@dave.com",
          :password => "another_pass"
      }
      last_response.should be_redirect
      follow_redirect!
      last_request.url.should == 'http://example.org/dave/dashboard'
    end
  end
Run Code Online (Sandbox Code Playgroud)

Sinatra应用程序上的post方法使用rest-client调用外部服务.我需要以某种方式存根其余的客户端调用以发回预设的响应,因此我不必调用实际的HTTP调用.

我的申请代码是......

  post '/user/create' do
    user_name = params[:name]
    response = RestClient.post('http://localhost:1885/api/users/', params.to_json, :content_type => :json, :accept => :json)
    if response.code == 200
      redirect to "/#{user_name}/dashboard"
    else
      raise response.to_s
    end
  end
Run Code Online (Sandbox Code Playgroud)

有人可以告诉我如何用RSpec做到这一点吗?我已经用Google搜索并发现了许多博客文章,这些文章从表面上划过,但我实际上找不到答案.我对RSpec时期很新.

谢谢

ruby rspec sinatra rest-client

11
推荐指数
2
解决办法
1万
查看次数

Insomnia REST Client - 为multipart/form-data设置"Content-Type"

TL; DR 我如何可以设置Content-Type每个标题单独文件/输入/文本中multipart/form-data(在失眠)的要求?

我正在尝试使用Insomnia Rest Client POST到OneNote API(HTTP描述).根据文档,我需要提交一个multipart/form-data带有标题的文件/文本的请求:

Content-Type: text/html Content-Disposition: form-data; name=presentation

另一个带标题:

Content-Type: application/inkml+xml Content-Disposition: form-data; name=presentation-onenote-inkml

这是我正在尝试的截图: 头 如您所见,API返回错误,No Content-Type导致我认为Content-Type未设置标头.调试信息如下: 调试 POST数据被隐藏,这使我无法看到Content-Type.

api rest rest-client onenote-api insomnia

11
推荐指数
1
解决办法
1万
查看次数