Konversi perintah curl ke kode Ruby - Hasilkan kode Ruby Net::HTTP siap pakai untuk permintaan 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
Berikut adalah beberapa perintah curl umum yang dapat Anda konversi ke kode 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
Library Ruby Net::HTTP adalah cara yang kuat untuk membuat permintaan HTTP. Berikut adalah beberapa pola Ruby Net::HTTP yang umum:
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
Salin perintah curl Anda → Tempel ke kotak input → Dapatkan kode Ruby Net::HTTP yang dikonversi
Konverter kami mendukung perintah curl kompleks dan menerjemahkannya ke kode Ruby yang bersih dan efisien menggunakan library Net::HTTP
Alat kami menangani opsi curl umum ini dan mengkonversinya ke kode Ruby Net::HTTP yang sesuai:
J: Kode Ruby Net::HTTP yang dihasilkan kompatibel dengan Ruby 2.0 dan di atasnya. Untuk versi Ruby yang lebih lama, penyesuaian kecil mungkin diperlukan.
J: Kode dasar yang dihasilkan tidak mencakup penanganan kesalahan yang ekstensif. Untuk kode produksi, Anda harus menambahkan blok begin/rescue untuk menangani pengecualian potensial seperti Net::HTTPError atau masalah koneksi.
J: Untuk respons JSON, gunakan JSON.parse(response.body) untuk mengurai respons menjadi hash Ruby. Untuk format lain, Anda dapat menggunakan response.body untuk konten mentah.
J: Library Net::HTTP adalah bagian dari library standar Ruby, jadi tidak diperlukan gem tambahan untuk permintaan HTTP dasar. Untuk penanganan JSON, gem 'json' disertakan dalam library standar sejak Ruby 1.9.
J: Untuk unggahan file di Ruby, Anda perlu menggunakan data form multipart dengan Net::HTTP. Konverter kami menangani perintah curl dengan opsi -F
atau --form
dan menghasilkan kode Ruby yang sesuai.
J: Library Ruby Net::HTTP menyediakan penanganan cookie melalui HTTP::Cookie jar. Ketika Anda mengkonversi perintah curl yang menyertakan penanganan cookie (menggunakan -b
atau --cookie
), alat kami menghasilkan kode Ruby yang mengelola cookie dengan benar.
J: Sementara curl sangat baik untuk pengujian API command-line yang cepat, Ruby Net::HTTP menyediakan pendekatan terprogram yang terintegrasi dengan aplikasi Ruby Anda. Mengkonversi curl ke Ruby membantu menjembatani kesenjangan antara pengujian dan implementasi dalam pengembangan Ruby.
Memahami perintah curl sangat penting untuk pengujian API yang efektif dengan Ruby. Berikut adalah referensi cepat opsi curl umum yang didukung konverter kami:
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 connectionKonverter Ruby kami menangani perintah curl kompleks termasuk beberapa header, autentikasi, muatan data, dan berbagai opsi. Cukup tempel perintah curl Anda dan dapatkan kode Ruby yang bersih dan modern menggunakan library Net::HTTP.
Saat bekerja dengan library Ruby Net::HTTP, ikuti praktik terbaik ini untuk interaksi API yang efisien dan aman:
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