我必须向API发送测试请求以进行验证:
def fake_xml
builder = Nokogiri::XML::Builder.new do |xml|
xml.TrackRequest.('USERID' => USPS_API["user_id"]) {
xml.TrackID('ID' => 'EJ958083578US')
}
end
puts builder.to_xml
return builder.to_xml
end
但是,这会产生:
<TrackRequest class="call" USERID="xxxx">
<TrackID ID="EJ958083578US"/>
</TrackRequest>
我不想要class="call"
,因为它会弄乱请求并导致它返回失败的响应。如何删除它或阻止Nokogiri自动添加该类?
答案 0 :(得分:3)
尝试如下:
require 'nokogiri'
builder = Nokogiri::XML::Builder.new do |xml|
xml.TrackRequest('USERID' => '12') {
xml.TrackID('ID' => 'EJ958083578US')
}
end
puts builder.to_xml
# >> <?xml version="1.0"?>
# >> <TrackRequest USERID="12">
# >> <TrackID ID="EJ958083578US"/>
# >> </TrackRequest>
否.
应该在xml.TrackRequest.('USERID' => USPS_API["user_id"])
。简单xml.TrackRequest('USERID' => USPS_API["user_id"])
。