แปลงคำสั่ง curl เป็นโค้ด Ruby - สร้างโค้ด Ruby Net::HTTP พร้อมใช้งานสำหรับคำขอ API
# Ruby Net::HTTP code will appear here # Example: require 'net/http' require 'uri' require 'json' uri = URI.parse('https://api.example.com/data') http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = true if uri.scheme == 'https' request = Net::HTTP::Post.new(uri.path) request['Content-Type'] = 'application/json' request.body = JSON.dump({name: 'test'}) response = http.request(request) puts response.code puts response.body
นี่คือคำสั่ง curl ทั่วไปที่คุณสามารถแปลงเป็นโค้ด Ruby ได้:
curl https://api.example.com/users
curl -X POST -H "Content-Type: application/json" -d '{"name":"John","email":"[email protected]"}' https://api.example.com/users
curl -X PUT -H "Authorization: Bearer token123" -d '{"status":"active"}' https://api.example.com/users/1
curl -X DELETE https://api.example.com/users/1
curl -H "X-API-Key: abc123" -H "Accept: application/json" https://api.example.com/data
ไลบรารี Net::HTTP ของ Ruby เป็นวิธีที่ทรงพลังในการทำคำขอ HTTP นี่คือรูปแบบ Ruby Net::HTTP ทั่วไป:
require 'net/http' require 'uri' uri = URI.parse('https://api.example.com/upload') http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = true if uri.scheme == 'https' request = Net::HTTP::Post.new(uri.path) request['Authorization'] = 'Bearer YOUR_TOKEN_HERE' # Create multipart form data boundary = "AaB03x" post_body = [] post_body << "--#{boundary}\r\n" post_body << "Content-Disposition: form-data; name=\"file\"; filename=\"document.pdf\"\r\n" post_body << "Content-Type: application/pdf\r\n\r\n" post_body << File.read('document.pdf') post_body << "\r\n--#{boundary}--\r\n" request['Content-Type'] = "multipart/form-data; boundary=#{boundary}" request.body = post_body.join response = http.request(request) puts response.body
require 'net/http' require 'uri' require 'json' uri = URI.parse('https://api.example.com/data') http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = true if uri.scheme == 'https' http.open_timeout = 5 # seconds http.read_timeout = 5 # seconds begin request = Net::HTTP::Get.new(uri.request_uri) response = http.request(request) case response when Net::HTTPSuccess data = JSON.parse(response.body) puts data else puts "Error: #{response.code} - #{response.message}" end rescue Net::OpenTimeout puts "Connection timed out" rescue Net::ReadTimeout puts "Response timed out" rescue StandardError => e puts "Error making request: #{e.message}" end
คัดลอกคำสั่ง curl ของคุณ → วางลงในช่องป้อนข้อมูล → รับโค้ด Ruby Net::HTTP ที่แปลงแล้ว
เครื่องมือแปลงของเรารองรับคำสั่ง curl ที่ซับซ้อนและแปลงเป็นโค้ด Ruby ที่สะอาดและมีประสิทธิภาพโดยใช้ไลบรารี Net::HTTP
เครื่องมือของเราจัดการตัวเลือก curl ทั่วไปเหล่านี้และแปลงเป็นโค้ด Ruby Net::HTTP ที่เหมาะสม:
คำตอบ: โค้ด Ruby Net::HTTP ที่สร้างขึ้นเข้ากันได้กับ Ruby 2.0 และสูงกว่า สำหรับ Ruby เวอร์ชันเก่ากว่า อาจต้องมีการปรับเล็กน้อย
คำตอบ: โค้ดพื้นฐานที่สร้างขึ้นไม่รวมการจัดการข้อผิดพลาดอย่างครอบคลุม สำหรับโค้ดการผลิต คุณควรเพิ่มบล็อก begin/rescue เพื่อจัดการข้อยกเว้นที่อาจเกิดขึ้นเช่น Net::HTTPError หรือปัญหาการเชื่อมต่อ
คำตอบ: สำหรับการตอบสนอง JSON ใช้ JSON.parse(response.body) เพื่อแยกวิเคราะห์การตอบสนองเป็น hash ของ Ruby สำหรับรูปแบบอื่นๆ คุณสามารถใช้ response.body สำหรับเนื้อหาดิบ
คำตอบ: ไลบรารี Net::HTTP เป็นส่วนหนึ่งของไลบรารีมาตรฐานของ Ruby ดังนั้นไม่จำเป็นต้องมี gems เพิ่มเติมสำหรับคำขอ HTTP พื้นฐาน สำหรับการจัดการ JSON gem 'json' รวมอยู่ในไลบรารีมาตรฐานตั้งแต่ Ruby 1.9
คำตอบ: สำหรับการอัปโหลดไฟล์ใน Ruby คุณจะต้องใช้ข้อมูลแบบฟอร์มหลายส่วนกับ Net::HTTP เครื่องมือแปลงของเราจัดการคำสั่ง curl ที่มีตัวเลือก -F
หรือ --form
และสร้างโค้ด Ruby ที่เหมาะสม
คำตอบ: ไลบรารี Net::HTTP ของ Ruby มีการจัดการคุกกี้ผ่าน HTTP::Cookie jar เมื่อคุณแปลงคำสั่ง curl ที่รวมการจัดการคุกกี้ (โดยใช้ -b
หรือ --cookie
) เครื่องมือของเราจะสร้างโค้ด Ruby ที่จัดการคุกกี้อย่างเหมาะสม
คำตอบ: ในขณะที่ curl เหมาะสำหรับการทดสอบ API บรรทัดคำสั่งอย่างรวดเร็ว Ruby Net::HTTP ให้วิธีการเขียนโปรแกรมที่ผสานรวมกับแอปพลิเคชัน Ruby ของคุณ การแปลง curl เป็น Ruby ช่วยเชื่อมช่องว่างระหว่างการทดสอบและการนำไปใช้ในการพัฒนา Ruby
การเข้าใจคำสั่ง curl มีความสำคัญสำหรับการทดสอบ API ที่มีประสิทธิภาพด้วย Ruby นี่คือข้อมูลอ้างอิงอย่างรวดเร็วของตัวเลือก curl ทั่วไปที่เครื่องมือแปลงของเรารองรับ:
curl [options] [URL]
-X, --request METHOD
: Specify request method (GET, POST, PUT, DELETE, etc.)-H, --header LINE
: Add header to the request-d, --data DATA
: Send data in POST request-F, --form CONTENT
: Submit form data-u, --user USER:PASSWORD
: Server user and password-k, --insecure
: Allow insecure server connections-I, --head
: Show document info only-v, --verbose
: Make the operation more verbose-s, --silent
: Silent mode--connect-timeout SECONDS
: Maximum time for connectionเครื่องมือแปลง Ruby ของเราจัดการคำสั่ง curl ที่ซับซ้อนรวมถึงหลาย headers, การตรวจสอบสิทธิ์, ข้อมูลที่ส่ง และตัวเลือกต่างๆ เพียงวางคำสั่ง curl ของคุณและรับโค้ด Ruby ที่สะอาดและทันสมัยโดยใช้ไลบรารี Net::HTTP
เมื่อทำงานกับไลบรารี Ruby Net::HTTP ให้ปฏิบัติตามแนวทางที่ดีที่สุดเหล่านี้สำหรับการโต้ตอบกับ API ที่มีประสิทธิภาพและปลอดภัย:
require 'net/http' require 'uri' uri = URI.parse('https://api.example.com') Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https') do |http| # First request request1 = Net::HTTP::Get.new('/users') response1 = http.request(request1) # Second request (uses same connection) request2 = Net::HTTP::Get.new('/products') response2 = http.request(request2) end
require 'net/http' require 'uri' uri = URI.parse('https://api.example.com/data') begin response = Net::HTTP.get_response(uri) case response when Net::HTTPSuccess puts "Success: #{response.body}" when Net::HTTPRedirection puts "Redirection to: #{response['location']}" when Net::HTTPClientError puts "Client error: #{response.code} - #{response.message}" when Net::HTTPServerError puts "Server error: #{response.code} - #{response.message}" else puts "Unknown response: #{response.code} - #{response.message}" end rescue SocketError => e puts "Connection error: #{e.message}" rescue Timeout::Error puts "Connection timed out" rescue StandardError => e puts "Error: #{e.message}" end
require 'net/http' require 'uri' require 'json' uri = URI.parse('https://api.example.com/data') response = Net::HTTP.get_response(uri) begin data = JSON.parse(response.body) puts data['name'] rescue JSON::ParserError => e puts "Invalid JSON response: #{e.message}" end