简体   繁体   English

Ruby:HTTParty:无法正确格式化 XML POST 数据?

[英]Ruby: HTTParty: can't format XML POST data correctly?

NOTE: "object" is a placeholder work, as I don't think I should be saying what the controller does specifically.注意:“对象”是一个占位符作品,因为我认为我不应该说 controller 具体做什么。

so, I have multiple ways of calling my apps API, the following works in the command line:所以,我有多种方式调用我的应用程序 API,以下在命令行中有效:

curl -H 'Content-Type: application/xml' -d '<object><name>Test API object</name><password>password</password><description>This is a test object</description></object>' "http://acme.example.dev/objects.xml?api_key=1234"

the above command generates the following request in the devlog:上述命令在 devlog 中生成以下请求:

Processing ObjectsController#create to xml (for 127.0.0.1 at 2011-07-07 09:17:51) [POST]
  Parameters: {"format"=>"xml", "action"=>"create", "api_key"=>"1234", "controller"=>"objects", 
  "object"=>{"name"=>"Test API object", "description"=>"This is a test object", "password"=>"[FILTERED]"}}

Now, I'm trying to write tests for the actions using the API, to make sure the API works, as well as the controllers.现在,我正在尝试使用 API 为操作编写测试,以确保 API 以及控制器都能正常工作。 Here is my current (broken) httparty command:这是我当前的(损坏的)httparty 命令:

  response = post("create", :api_key => SharedTest.user_api_key, :xml => data, :format => "xml")

this command generates the following request in the testlog:此命令在测试日志中生成以下请求:

Processing ObjectsController#create to xml (for 0.0.0.0 at 2011-07-07 09:37:35) [POST]
  Parameters: {
        "xml"=>"<object><name><![CDATA[first post]]></name>
                    <description><![CDATA[Things are not as they used to be]]></description>
                    <password><![CDATA[WHEE]]></password>
                </object>", 
                "format"=>"xml", 
                "api_key"=>"the_hatter_wants_to_have_tea1", 
                "action"=>"create", 
                "controller"=>"objects

So, as you can see, the command line command actually generates the object hash from the xml, whereas the httparty command ends up staying in xml, which causes problems for the create method, as it needs a hash. So, as you can see, the command line command actually generates the object hash from the xml, whereas the httparty command ends up staying in xml, which causes problems for the create method, as it needs a hash.

Any ideas / proper documentation?任何想法/适当的文档? Current documentation says that post takes an url, and "options" and then never says what options are available当前文档说该帖子采用 url 和“选项”,然后从未说明可用的选项


* *EDIT : * *编辑
as per @Casper's suggestion, my method now looks like this:根据@Casper 的建议,我的方法现在如下所示:

def post_through_api_to_url(url, data, api_key = SharedTest.user_api_key)

  response = post("create", {
    :query => {
      :api_key => api_key
    },
    :headers => {
      "Content-Type" => "application/xml"
    },
    :body => data
  })
  ap @request.env["REQUEST_URI"]
  assert_response :success

  return response
end

unfortunately, the assert_response fails, because the authentication via the api key fails.不幸的是,assert_response 失败,因为通过 api 密钥的身份验证失败。 looking at the very of of the request_uri, the api_key isn't being set properly... it shows:查看 request_uri 的内容,api_key 设置不正确......它显示:

api_key%5D=the_hatter_wants_to_have_tea1"

but it should just be equals, without the %5D (right square bracket)但它应该只是等于,没有 %5D (右方括号)

I think this is how you're supposed to use it:我认为这就是你应该如何使用它:

options = {
  :query => {
    :api_key => 1234
  },

  :headers => {
    "Content-Type" => "application/xml"
  },

  :body => "<xmlcode>goes here</xmlcode>"
}

post("/create", options)

Forgive me for being basic about it but if you only want to send one variable as a parameter, why don't you do as Casper suggests, but just do:请原谅我是基本的,但如果你只想发送一个变量作为参数,你为什么不按照 Casper 的建议做,而只是做:

post("/create?api_key=1234", options)

Or rather than testing HTTParty's peculiarities in accessing your API, perhaps write your tests using Rack::Test ?或者,与其测试 HTTParty 在访问 API 时的特性,不如使用Rack::Test编写测试? Very rough example...很粗略的例子...

require "rack/test"
require "nokogiri"

class ObjectsTest < Test::Unit::TestCase
  include Rack::Test::Methods

  def app
    MyApp.new
  end

  def create_an_object(o)
    authorize "x", "1234" # or however you want to authenticate using query params
    header 'Accept', 'text/xml'
    header 'Content-Type', 'text/xml'
    body o.to_xml
    post "/create"

    xml = Nokogiri::XML(last_response.body)
    assert something_logic_about(xml)
  end

end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM