Rails API POST(XML) - 不起作用

时间:2016-07-01 09:30:43

标签: ruby-on-rails xmlhttprequest http-post

我正在尝试向远程API(外部网站)发布帖子请求,该API接受XML格式的帖子参数。我有以下代码:

def advertiser_exists
      uri = URI.parse(@api_url)
      request = Net::HTTP::Post.new(uri.path)
      xml_string = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><request><key>#{@api_key}</key><method>GetCampaigns</method><partner_id>#{@account_id}</partner_id></request>"
      request.body = xml_string
      request.content_type = 'text/xml'
      response = Net::HTTP.new(uri.host, uri.port).start { |http| http.request request }
end

但回应不是,我期待的是:

"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\t\t\t<response>\n\t\t\t\t<status>ERROR</status>\n\t\t\t\t<reason>No Data</reason>\n\t\t\t</response>"

这是PHP版本(由提供商提供,应该有效):

<?php
    // url api
    $url = "http://YOUR_URL/api/";

    // request
    $xml = '<?xml version="1.0" encoding="UTF-8"?> 
    <request>
        <key>abcdefghijklm</key>
        <method>AddConversion</method>      
        <partner_id>1</partner_id>
        <campaign_id>1</campaign_id>
        <conversion_id>1</conversion_id>
        <value>100</value>
    </request>';

    // sending request 
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, "data=".$xml);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $output = curl_exec($ch);
    curl_close($ch);

    // response
    echo $output;
?>

任何想法,我上面的代码有什么问题(参数是正确的,应该有响应数据)?

谢谢,Miroslav

0 个答案:

没有答案