如何将curl命令转为Ruby(Net::HTTP)带Header的XML格式HTTP POST请求?
Got it, let's break down how to convert that curl XML POST request into working Rails code using Net::HTTP. I'll cover the common hurdles that might be stumping you right now.
First, let's assume a sample curl command (since you didn't share yours, this mirrors a typical API call with headers and XML payload):
curl -X POST https://api.yourservice.com/submit \ -H "Content-Type: application/xml" \ -H "X-API-Key: YOUR_SECRET_KEY" \ -d '<Customer><Name>John Doe</Name><Email>john@example.com</Email></Customer>'
Step-by-Step Net::HTTP Implementation
Here's the equivalent Rails code, with explanations for each part:
require 'net/http' require 'uri' require 'openssl' # Needed for HTTPS requests # 1. Define your API endpoint and payload uri = URI('https://api.yourservice.com/submit') xml_payload = '<Customer><Name>John Doe</Name><Email>john@example.com</Email></Customer>' # 2. Initialize the HTTP client (handle HTTPS properly) http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = true # Optional: Only disable verification if you're dealing with a self-signed cert (NOT for production!) # http.verify_mode = OpenSSL::SSL::VERIFY_NONE # 3. Build the POST request with headers request = Net::HTTP::Post.new(uri) # Critical: Set Content-Type to application/xml so the server knows how to parse your payload request['Content-Type'] = 'application/xml' # Add any other custom headers from your curl command request['X-API-Key'] = 'YOUR_SECRET_KEY' # 4. Attach the XML payload request.body = xml_payload # 5. Send the request and handle the response response = http.request(request) # 6. Process the result case response when Net::HTTPSuccess # Parse the XML response if needed (use Nokogiri for easier parsing) require 'nokogiri' parsed_response = Nokogiri::XML(response.body) puts "Request succeeded! Response: #{parsed_response}" else puts "Request failed. Status code: #{response.code}, Message: #{response.message}" puts "Response body: #{response.body}" end
Common Bottlenecks (And Fixes!)
If you're hitting issues, check these first:
- Missing Content-Type header: This is the #1 mistake. Without
application/xml, the server will likely reject your payload or parse it incorrectly. - HTTPS certificate errors: If you're testing against a dev server with a self-signed cert, add
http.verify_mode = OpenSSL::SSL::VERIFY_NONE(but remove this in production!). - Unescaped XML characters: If your payload has special characters like
<,>, or&, use a library like Nokogiri to generate valid XML instead of hardcoding strings:xml = Nokogiri::XML::Builder.new do |xml| xml.Customer do xml.Name "John O'Doe" # Automatically escapes special characters xml.Email "john@example.com" end end.to_xml - Incorrect endpoint URI: Double-check that your URI includes the full path (e.g.,
/submit), not just the domain. - Response handling: Don't ignore the response body—even error responses often include XML with details about what went wrong.
内容的提问来源于stack exchange,提问作者Jedidiah
相关产品推荐
相关产品推荐

