尝试使用XML正文将发布请求发送到加拿大发布API

时间:2019-05-22 19:29:45

标签: ruby-on-rails xml http-post rest-client

我正在尝试使用rest-client向加拿大的邮政评分api发送发帖请求(以计算运费):

xml_data =
        %{<?xml version='1.0' encoding='UTF-8'?>
        <mailing-scenario xmlns='http://www.canadapost.ca/ws/ship/rate-v4'>
          <customer-number>#{ENV['CP_CUSTOMER_NUMBER']}</customer-number>
          <contract-id>#{ENV['CP_CONTRACT_ID']}</contract-id>
          <options>
            <option>
              <option-code>SO</option-code>
            </option>
          </options>
          <parcel-characteristics>
            <weight>#{total_weight}</weight>
            <dimensions>
              <length>30</length>
              <height>20</height>
              <width>10</width>
            </dimensions>
          </parcel-characteristics>
          <origin-postal-code>#{ENV['ZIP']}</origin-postal-code>
          <destination>
            <domestic>
              <postal-code>#{shipping_address.zip.upcase.delete(' ')}</postal-code>
            </domestic>
          </destination>
        </mailing-scenario>}

    response =
        RestClient.post(
            "#{'ct.' if !Rails.env.production?}soa-gw.canadapost.ca",
            Nokogiri::XML(xml_data).to_xml,
            {
              :'Accept' => 'application/vnd.cpc.ship.rate-v4+xml',
              :'Content-Type' => 'application/vnd.cpc.ship.rate-v4+xml',
              :'Authorization' => "Basic #{Rails.env.production? ? ENV['CP_PRODUCTION_CREDENTIAL'] : ENV['CP_DEVELOPMENT_CREDENTIAL']}",
              :'Accept-Language' => 'en-CA'
            }
        )

但RestClient.post抛出错误RestClient::MovedPermanently (301 Moved Permanently),显然表明网址中有错字。

但是,此请求在Postman中可以正常工作。我的格式有问题吗?

生成的xml看起来不错,所以我担心我不了解如何格式化RestClient对象。

这里的解决方案是什么?感谢您的协助。

1 个答案:

答案 0 :(得分:0)

URL确实不正确!还是谢谢你!