Simple ruby TCP request doesn't hit the server -


i have simple ruby script upload file server/web app through tcp connection doesn't work.when run script nothing happens on web app/server side.the server works fine have tried upload file curl , did upload.take @ code below , let me know doing wrong.i using ruby 1.9.2-p290.thank in advance.

require 'socket'     host = "myapp.herokuapp.com" port = 80  client = tcpsocket.open(host, port)  client.print("post /api/binary http/1.1\r\n") client.print("host: myapp.herokuapp.com\r\n") client.print ("accept: */* \r\n") client.print ("content-type: multipart/form-data;boundary=aab03x \r\n")   client.print("\n" + "aab03x"+ "\n" "content-disposition: form-data; name='datafile'; filename='cam.jpg' \n content-type: image/jpeg \r\n") client.print ("\r\n") data = file.open("./pic.jpg", "rb") {|io| io.read} client.print (data) client.print ("\r\n")  client.print("boundary=aab03x\r\n")  client.close 

irb console

>require 'socket'  => true  >  client = tcpsocket.open("myapp.herokuapp.com", 80)  => #<tcpsocket:fd 3> > client.print("get /api/users http/1.1")   => nil  > client.print("post /api/binary http/1.1")   => nil  

you need make sure sending valid http request.

  1. you need content-length header. means need assemble body in advance can determine length header, send body. if wrong server end blocking trying read more input doesn’t come.

  2. your multipart boundaries need fixing. should start --, token header: --aab03x. final 1 should end -- too: --aab03x--. make sure there’s no trailing whitespace in header well, can cause issues.

some other things won’t prevent request being parsed should tidy up:

  1. newlines in headers should \r\n not \n.

  2. header lines shouldn’t have whitespace before them.

require 'socket'     host = "myapp.herokuapp.com" port = 80  client = tcpsocket.open(host, port)  # write out headers. client.print("post /api/binary http/1.1\r\n") client.print("host: myapp.herokuapp.com\r\n") client.print ("accept: */* \r\n") # note: no trailing whitespace. client.print ("content-type: multipart/form-data;boundary=aab03x\r\n")  # assemble body. # note \r\n line endings, body doesn't start newline. # boundary marker starts '--'. body = "--aab03x\r\n" body << "content-disposition: form-data; name='datafile'; filename='cam.jpg'\r\n" # header starts @ left of line, no leading whitespace. body << "content-type: image/jpeg\r\n" body << "\r\n" data = file.open("./pic.jpg", "rb") {|io| io.read} body << data body << "\r\n" # final boundary marker has trailing '--' body << "--aab03x--\r\n"  # can write content-length header, since # know size of body. client.print "content-length: #{body.bytesize}\r\n" # blank line. client.print "\r\n" # write out body. client.print body  # in reality want parse response # server before closing socket. client.close 

Comments

Popular posts from this blog

scala - 'wrong top statement declaration' when using slick in IntelliJ -

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

PySide and Qt Properties: Connecting signals from Python to QML -