如何通过Ruby基本身份验证传递值和标头?

时间:2014-08-12 20:10:30

标签: ruby http basic-authentication

我试图通过Ruby传递某些值和标题但不确定如何操作。到目前为止我所拥有的:

  uri = URI.parse('http://jira.test.local/rest/zapi/latest/execution')

  req = Net::HTTP::Get.new(uri)
  req.basic_auth 'userid', 'password'
  res = Net::HTTP.start(uri.hostname, uri.port) {|http| http.request(req)}
  puts res.body

  values   = "{\n    \"issueId\": 32640,\n    \"versionId\": \"11163\",\n    \"cycleId\": \"5\",\n    \"projectId\": 10460\n,\n    \"status\": \"1\"}"
  headers  = {:content_type => "application/json"}

  Net::HTTP.start(uri.hostname, uri.port) do | http |
          response = http.post(req, values)
          puts response.body
  end

我现在收到此错误:

{"status":{"1":{"id":1,"color":"#75B000","description":"Test was executed and passed successfully.","name":"PASS"},"2":{"id":2,"color":"#CC3300","description":"Test was executed and failed.","name":"FAIL"},"3":{"id":3,"color":"#F2B000","description":"Test execution is a work-in-progress.","name":"WIP"},"4":{"id":4,"color":"#6693B0","description":"The test execution of this test was blocked for some reason.","name":"BLOCKED"},"-1":{"id":-1,"color":"#A0A0A0","description":"The test has not yet been executed.","name":"UNEXECUTED"}},"executions":[],"currentlySelectedExecutionId":""}
      undefined method `empty?' for #<Net::HTTP::Get GET> (NoMethodError)
      /Users/fyousuf/.rvm/rubies/ruby-2.0.0-p481/lib/ruby/2.0.0/net/http/generic_request.rb:27:in `initialize'
      /Users/fyousuf/.rvm/rubies/ruby-2.0.0-p481/lib/ruby/2.0.0/net/http/request.rb:14:in `initialize'
      /Users/fyousuf/.rvm/rubies/ruby-2.0.0-p481/lib/ruby/2.0.0/net/http.rb:1390:in `new'
      /Users/fyousuf/.rvm/rubies/ruby-2.0.0-p481/lib/ruby/2.0.0/net/http.rb:1390:in `send_entity'
      /Users/fyousuf/.rvm/rubies/ruby-2.0.0-p481/lib/ruby/2.0.0/net/http.rb:1179:in `post'
      ./features/step_definitions/zapi_farooq.rb:32:in `block (2 levels) in <top (required)>'
      /Users/fyousuf/.rvm/rubies/ruby-2.0.0-p481/lib/ruby/2.0.0/net/http.rb:852:in `start'
      /Users/fyousuf/.rvm/rubies/ruby-2.0.0-p481/lib/ruby/2.0.0/net/http.rb:582:in `start'
      ./features/step_definitions/zapi_farooq.rb:31:in `/^I test zapi update$/'
      features/zapi_farooq.feature:4:in `* I test zapi update'

输出的第一行是好的,它给了我来自res.body的正确的outpul,但之后是错误。

摘要:我想转到uri,使用creds进行身份验证并使用提供的值和正确的标题发布(所有使用Ruby)。

我尝试根据此API创建新的执行:http://docs.getzephyr.apiary.io/#executionresourceapis(创建新的执行)

1 个答案:

答案 0 :(得分:0)

到处玩,我得到了它的工作:

  values   = "{\n    \"status\": 2\n}"
  headers  = {:content_type => "application/json"}
  response = RestClient::Resource.new 'http://jira.test.local/rest/zapi/latest/execution/343/execute', 'username', 'password'
  response.put values, headers
  puts response
  puts response.body

输出:

  http://jira.test.local/rest/zapi/latest/execution/343/execute
  <html xmlns="http://www.w3.org/1999/xhtml"><head></head><body></body></html>

此外,它还会更改Jira中的问题值(预期目标)。

我刚刚使用RestClient b / c原始文档(http://docs.getzephyr.apiary.io/#executionresourceapis)正在使用它并在此处获得了一些帮助:https://github.com/rest-client/rest-client