roysathome.net/auth/post.rb

96 lines
2.4 KiB
Ruby
Raw Normal View History

2023-12-11 20:56:09 +00:00
require 'net/http'
require 'net/http/post/multipart'
2023-12-11 20:56:09 +00:00
require 'uri'
2023-12-14 00:18:12 +00:00
require 'json'
2023-12-11 20:56:09 +00:00
# The URL you want to send the POST request to
url = URI.parse('http://auth.roysathome.net/auth/login')
2023-12-11 20:56:09 +00:00
# Create a new Net::HTTP object with the target server
http = Net::HTTP.new(url.host, url.port)
# If your server uses HTTPS, you might need to enable SSL
# http.use_ssl = true
# Create a new Net::HTTP::Post request with the desired path
request = Net::HTTP::Post.new(url.path)
puts 'Enter username:'
username = gets.strip!
puts 'Enter password'
password = gets.strip!
2023-12-11 20:56:09 +00:00
# Set the request body with the data you want to send
request.body = "username=#{username}&password=#{password}"
#request.body = "username=admin&password=pass123"
2023-12-11 20:56:09 +00:00
# Set the 'Content-Type' header if needed
request['Content-Type'] = 'application/x-www-form-urlencoded'
# Send the request and get the response
response = http.request(request)
2023-12-14 00:18:12 +00:00
unless response.code == '200'
puts "Response Code: #{response.code}. Aborting."
puts "Response Body: #{response.body}"
puts "Aborting."
exit
end
puts 'Reauthentication token stored.'
response_data = JSON.parse(response.body)
reauthentication_token = response_data['token']
#while true
url = URI.parse('http://auth.roysathome.net/auth/reauthenticate')
request = Net::HTTP::Post.new(url.path)
2023-12-14 00:18:12 +00:00
request['Authorization'] = "Bearer #{reauthentication_token}"
2023-12-14 00:18:12 +00:00
response = http.request(request)
2023-12-14 00:18:12 +00:00
unless response.code == '200'
puts "Response Code: #{response.code}. Aborting."
puts "Response Body: #{response.body}"
puts "Aborting."
exit
end
#puts response.body
response_data = JSON.parse(response.body)
authentication_token = response_data['jwt']
2023-12-14 00:18:12 +00:00
#end
############# Do Files Test
#sleep(2)
2023-12-14 00:18:12 +00:00
# Read the file contents
file_content = File.read('./example.txt')
# Create a URI object for the API endpoint
uri = URI.parse('http://auth.roysathome.net/upload')
# Create a Net::HTTP object
http = Net::HTTP.new(uri.host, uri.port)
request = Net::HTTP::Post::Multipart.new(
uri.path,
'file' => UploadIO.new(StringIO.new(file_content), 'application/octet-stream', 'example.txt')
)
request['Authorization'] = "Bearer #{authentication_token}"
# Send the request
puts "Sending file of size #{(File.size('./example.txt') / 2**20).round(2)} MB"
response = http.request(request)
#puts response.code
unless response.code == '200'
puts response.code
else
puts response.body
end