Skip to content

Instantly share code, notes, and snippets.

View mensfeld's full-sized avatar
๐Ÿ’ญ
๐Ÿš€ ๐Ÿš€ ๐Ÿš€ ๐Ÿš€ ๐Ÿš€

Maciej Mensfeld mensfeld

๐Ÿ’ญ
๐Ÿš€ ๐Ÿš€ ๐Ÿš€ ๐Ÿš€ ๐Ÿš€
View GitHub Profile
module NetHttpFaradayPatch
def create_request(env)
super.tap do |request|
if env[:body].respond_to?(:read)
request.content_length = env[:body].size
end
end
end
end
Net::HTTP::Post
.new('/', 'Content-Type': 'application/json')
.tap { |req| req.body_stream = StringIO.new(operation) }
.tap { |req| req.content_length = operation.bytesize }
.yield_self(&@http.method(:request))
.yield_self(&:body)
puts client.get #=> []
puts client.add('12313131') #=> ['12313131']
puts client.add('msg') #=> ['12313131', 'msg']
puts client.del('msg') #=> ['12313131']
require 'net/http'
class Api
HOST = 'localhost'
PORT = 3000
def initialize
@http = Net::HTTP.new(HOST, PORT)
end
ruby server.rb
[2019-01-09 22:38:58] INFO WEBrick 1.4.2
[2019-01-09 22:38:58] INFO ruby 2.6.0 (2018-12-25)
[2019-01-09 22:38:58] INFO WEBrick::HTTPServer
require 'webrick'
require 'set'
server = WEBrick::HTTPServer.new(Port: 3000)
set = Set.new
server.mount_proc '/' do |req, res|
data, action = req.body.split(',')
action ||= 'ADD'
client = Api.new
client.get #=> []
client.add('12313131') #=> ['12313131']
client.add('msg') #=> ['12313131', 'msg']
client.del('msg') #=> ['12313131']
Net::HTTP.post(URI('http://httpbin.org/post'), 'ใ‚'*100_000)
Traceback (most recent call last):
16: from /net/http.rb:502:in `block in post'
15: from /net/http.rb:1281:in `post'
14: from /net/http.rb:1493:in `send_entity'
13: from /net/http.rb:1479:in `request'
12: from /net/http.rb:1506:in `transport_request'
11: from /net/http.rb:1506:in `catch'
10: from /net/http.rb:1507:in `block in transport_request'
9: from /net/http/generic_request.rb:123:in `exec'
sudo apt-get install sqlitebrowser