HTTP Post

In notebook:
FrontEndMasters Networking and Streams
Created at:
2017-09-23
Updated:
2017-09-23
Tags:
backend Fundamentals

http post

Forms in html are often delivered with a POST:

POST /form HTTP/1.1  ☛ note the POST word
Host: localhost
Content-Length: 51
Content-Type: application/x-www-form-urlencoded ☛ for most servers, you need to set the content-type

title=whatever&date=1421044443&body=beep%20boop%21  ☛ so the server can interpret correctly this formatting

using this simple node http server, we can decode the POST body: this helps to parse the form data.

var http = require('http')
var parseform = require('body/any')

var server = http.createServer(function (req, res) {
  console.log(req.method, req.url, req.heders)
  parseform(req, res, function (err, params) {
    console.log(params)
    res.end('ok\n')
  })
})
server.listen(5000)

He sends the payload:

$ nc localhost 5000 < post.txt ☛ post.txt is the same stuff as above

When the POST payload arrives and is decoded, we get:

$ node server.js
POST /form { host: 'localhost, 
  'content-length: '51',
  'content-type': 'application/x-www-form-url-encoded`,
  { title: 'whatever', date: '1421044443', body: 'beep boop!\n' } 
}

and the server responds with:

HTTP/1.1 200 OK
Date: Mon, 12 Jan 2015 06:37:51 GMT
Connection: keep-alive
Transfer-Encoding: chunked ☛ an alternative to content link header, see the responses below: ↴

3
ok

0

This way the server doesn't have to know in advance how much info it has to send back. 0 means, end, no more data