简体   繁体   English

使用Post and Get将cURL转换为Ruby net / http

[英]cURL to Ruby net/http with Post and Get

I am attempting to convert a cURL request into its Ruby equivalent. 我正在尝试将cURL请求转换为其等效的Ruby。 This is the cURL request and the xml file it is referencing is the same as the xml variable below; 这是cURL请求,它所引用的xml文件与下面的xml变量相同;

curl -H "X-SFDC-Session: sessionId" -H "Content-Type: application/xml; charset=UTF-8" -d @create-job.xml https://instance.salesforce.com/services/async/30.0/job

This is the ruby method I have come up with; 这是我想出的红宝石方法。

def create_salesforce_batch_job(object_name, salesforce_token)
  xml = <<-XML
  <?xml version="1.0" encoding="UTF-8"?>
  <jobInfo
      xmlns="http://www.force.com/2009/06/asyncapi/dataload">
    <operation>query</operation>
    <object>#{object_name}</object>
    <concurrencyMode>Parallel</concurrencyMode>
    <contentType>CSV</contentType>
  </jobInfo>
  XML
  uri = URI.parse("https://na1.salesforce.com/services/async/30.0/job/")
  headers = {'X-SFDC-Session' => salesforce_token, 'Content-Type' => "application/xml; charset=UTF-8"}
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true
  request = Net::HTTP::Get.new(uri.request_uri, headers)
  request.body = xml

  response = http.request(request)
end

but I keep getting HTTPBadRequest responses when I use my Ruby method, using the same credentials and xml with cURL returns the expected values. 但是当我使用Ruby方法时,我不断收到HTTPBadRequest响应,使用相同的凭据和带有cURL的xml返回期望值。 Where am I going wrong? 我要去哪里错了? Thank you! 谢谢!

Why don't you use the cURB gem ? 您为什么不使用cURB gem It's a libcurl wrapper, which maps more closely to cURL than Net::HTTP will. 它是一个libcurl包装器,与Net :: HTTP相比,它与cURL的映射更加紧密。

There are a number of examples, from simple GET to more complex, including showing how to use authentication and set headers. 从简单的GET到更复杂的示例很多,包括显示如何使用身份验证和设置标头。

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

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