Shop OBEX P1 Docs P2 Docs Learn Events
How to submit a POST or GET — Parallax Forums

How to submit a POST or GET

Mike GMike G Posts: 2,702
edited 2011-07-14 20:29 in Accessories
There has been a few posts lately asking how to submit an HTTP POST or GET. I set up a test page so you guys can test your code.

http://www.agaverobotics.com/spinneret/getpost.aspx

IP : PORT = 65.98.8.151:80
Edit: the IP and Port must be set when initializing a W5100 socket

The page will echo Query String and/or Form name values pairs back to the requester.

For those of you using my server code, paste the code block below to your project file and call the GetPost(id) method. Otherwise, the StringSend lines show what info to send to the server.
PUB GetPost(id) | st, size, idx, tempMask
  pst.str(string(13,"GET POST!",13))
  tempMask := tcpMask
  tcpMask := SetTcpSocketMaskById(id, 0)

  Socket.Close(id)
  pause(delay)
  InitializeSocket(id)
  pause(delay)

  'Get My IP
  pst.str(string(13, "Getting Assigned IP ",13))
  Socket.Connect(id)
  pause(delay)
  repeat while !Socket.Connected(id)

  pst.str(string(13, "Sending Header ",13))
  
  'spinneret/myip.aspx
  'spinneret/datetime.aspx
  '/spinneret/getpost.aspx?id=1&test=2
  StringSend(id, string("POST /spinneret/getpost.aspx?id=1&test=2 HTTP/1.1", 13, 10))
  StringSend(id, string("Keep-Alive: 115", 13, 10))
  StringSend(id, string("Host: agaverobotics.com", 13, 10))
  StringSend(id, string("Connection: keep-alive", 13, 10))
  StringSend(id, string("Content-Type: application/x-www-form-urlencoded", 13, 10)) 
  StringSend(id, string("Content-length: 23", 13, 10, 13, 10))
  StringSend(id, string("name=test&submit=Submit"))

  
  repeat until size := Socket.rxTCP(id, @rxdata)
  'Find and print the message body
  idx := str.MatchPattern(@rxdata, string(13,10,13,10), 0, true)
  pst.str(@rxdata+idx+4)

  'Reset the socket
  Socket.Disconnect(id)
  InitializeSocket(id)
  
  'Reset the tcpMask
  tcpMask := tempMask

For POSTing, the important header lines to note are...
Content-Type: application/x-www-form-urlencoded
Content-length: 23

test&submit=Submit

For a GET, add ?name=value&name1=value1 to the end requested resource (url).
POST /spinneret/getpost.aspx?id=1&test=2 HTTP/1.1
Sign In or Register to comment.