http.post dropping trailing '}'

W

Wilson Chen

I am making a call through http.post in Ruby 1.8.5.

uri = ...
data = '{}'
headers = ...
http.post(uri, data, headers)

Everything seems to work except that the trailing '}' in data is not
getting posted, as determined by the network analyzer WireShark. My
entire posted data is just '{'. Is there some sort of sanitization or
choping that I am unaware of?

Thanks.
 
P

Phillip Gawlowski

Wilson said:
I am making a call through http.post in Ruby 1.8.5.

uri = ...
data = '{}'
headers = ...
http.post(uri, data, headers)

Everything seems to work except that the trailing '}' in data is not
getting posted, as determined by the network analyzer WireShark. My
entire posted data is just '{'. Is there some sort of sanitization or
choping that I am unaware of?


you could either try String#dump to automagically create escape
characters, or escape the {} yourself with a backslash.

I have no clue, though, if that is feasible in regard to the receiving end.

--
Phillip "CynicalRyan" Gawlowski

Rule of Open-Source Programming #11:

When a developer says he will work on something, he or she means
"maybe".
 
B

Brian Candler

--/04w6evG8XlLl3ft
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline

I am making a call through http.post in Ruby 1.8.5.

uri = ...
data = '{}'
headers = ...
http.post(uri, data, headers)

Everything seems to work except that the trailing '}' in data is not
getting posted, as determined by the network analyzer WireShark. My
entire posted data is just '{'. Is there some sort of sanitization or
choping that I am unaware of?

Attached is a test case which proves everything is working OK, for me
anyway:

$ ruby posttest.rb
Listening on port 38449
<< request >>
POST /foo/bar HTTP/1.1
Accept: */*
Content-Type: text/plain
Content-Length: 2
Host: 127.0.0.1:38449

<< end headers >>
"{}"
Response: #<Net::HTTPServerError 500 Go away readbody=true>

This is ruby 1.8.4 (2005-12-24) [i486-linux] under Ubuntu 6.06

So I think you're probably misinterpreting your wireshark output, in which
case maybe you should post it. Better still, post tcpdump -s0 -X -n output.

Regards,

Brian.

--/04w6evG8XlLl3ft
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment; filename="posttest.rb"

require 'socket'

module Demo
class SimpleServer

attr_reader :thread, :server

def port
af, port, host, addr = @server.addr
return port
end

def initialize(opt = {}, &blk)
@server = TCPServer.new(opt[:addr] || '127.0.0.1', opt[:port])
@logger = opt[:logger] || $stderr
@call = blk || opt[:call] || (raise "No block or :call given")
@thread = nil
start
end

def start
@thread ||= Thread.new do
while sock = @server.accept
Thread.new(sock) do |s|
begin
@call.call(s)
rescue Exception => e
@logger << "Exception: #{e}\n#{e.backtrace.join("\n")}\n"
ensure
s.close rescue nil
end
end
end
end
end

def stop
@thread.kill if @thread
rescue ThreadError
ensure
@thread = nil
end
end # TCPServer
end # Demo

if __FILE__ == $0
ss = Demo::SimpleServer.new do |s|
$stderr.puts "<< request >>"
len = 0
while line = s.gets
$stderr.puts line
len = $1.to_i if /^content-length: *(\d+)/i =~ line
break if /^\s*$/ =~ line
end
$stderr.puts "<< end headers >>"
body = s.read(len)
$stderr.puts body.inspect
s.puts "HTTP/1.0 500 Go away"
end
port = ss.port
$stderr.puts "Listening on port #{port}"

require 'net/http'
Net::HTTP.start('127.0.0.1', port) do |http|
data = '{}'
headers = { 'Content-Type' => 'text/plain' }
resp = http.post('/foo/bar', data, headers)
$stderr.puts "Response: #{resp.inspect}"
end

ss.stop
end # if __FILE__ == $0

--/04w6evG8XlLl3ft--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,239
Messages
2,571,199
Members
47,835
Latest member
CyrusRuggi

Latest Threads

Top