Shop OBEX P1 Docs P2 Docs Learn Events
POST HEAD GET 2 way communication — Parallax Forums

POST HEAD GET 2 way communication

FredBlaisFredBlais Posts: 370
edited 2011-07-17 10:14 in Accessories
Hi there,

does someone here have an object that have all the included functions to do http?
I know that get and post request and sending the requested data is done (we have working http servers)
but what I would want is the spinneret to issue http post and get to a web server and receive data.
i.e. Sending sensor data to a web server so it can store it in a database.

also, does opening a socket with tcp support dns or static ip address?

~Fred

Comments

  • Mike GMike G Posts: 2,702
    edited 2011-07-13 05:30
    Fred, client requests work the same as server connections only in the opposite direction. Below is a snippet of code that I use to retrieve my WAN IP address from a server on the Internet. My Spinneret server code uses all 4 sockets so I have to take one socket out of rotation and initialize the socket for a client request. You probably won't have to do that but that's what this does...

    tcpMask := SetTcpSocketMaskById(id, 0)

    The rest is easy, just build the request header, send the request, and wait for the response. I'm doing a GET. To do a POST, add your name value pairs or data stream to the body of the request two lines (10,13,10,13) after the header. If you plan to call the same server several time, put the header string is a DAT section.

    See the W5100 manual for client mode operation.
    PUB GetMyIp(id) | st, size, idx, tempMask
    
      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)
    
      'spinneret/myip.aspx
      'spinneret/datetime.aspx
      StringSend(id, string("GET /spinneret/myip.aspx 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, 13, 10))
    
      
      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
    
    also, does opening a socket with ftp support dns or static ip address?
    Spinneret sockets communicate using TCP or UDP. FTP and DNS are protocols that ride on TCP and UDP.
  • FredBlaisFredBlais Posts: 370
    edited 2011-07-13 06:51
    Thanks, this is what I was looking for.

    I made a mistake for my question about dns... I meant tcp, not ftp.
    So if I understood, we have to specify the host in the get header like this : StringSend(id, string("Host: agaverobotics.com", 13, 10))
    So I do not need to know the ip address of the host, I just have to know the url?

    Hey, its my 100th post! :) first star!
  • Mike GMike G Posts: 2,702
    edited 2011-07-13 11:36
    Happy 100th post :)

    No No... you need the remote host IP. The remote IP is part of the socket initialization. Socket.Connected(id) causes the Spinneret to request a socket connection on the remote host. On success, the Spinneret and remote host establish an end-to-end TCP socket connection. The next thing to do is send an HTTP message to the remote host. The "Host:" header line tells the remote host which application to fire up. Line 1 contains the requested resource and is parsed, processing happens, and a response is sent back to the Spinneret.

    See the W5100 documentation for flows and check out the HTTP protocol.
  • worthyamherstworthyamherst Posts: 46
    edited 2011-07-14 11:45
    I want to take strings of data and send them to a PHP script on my web server. I don't quite understand how to use the Spinneret to do any of this though. I know I need to open a socket, connect to the IP of the server (which does not have a static IP. The only IP address that I have prompts to enter the FTP user name and password. Is it possible to program the spinneret to send the login verification?), and send the data packets.
  • Mike GMike G Posts: 2,702
    edited 2011-07-14 12:48
    @worthyamherst, What's the URL you are trying to hit?

    Please post your PHP script.
  • FredBlaisFredBlais Posts: 370
    edited 2011-07-14 18:20
    @worthyamherst

    please keep me updated with that, my needs are similars
  • worthyamherstworthyamherst Posts: 46
    edited 2011-07-17 09:00
    I'm trying to send data to http://wbuffalo.workforceinnovators.org/ I'm just using a basic GET or POST script that writes the data to a text file on the server. I'm confident in my ability to write the PHP scripts....I just don't know how to configure the Spinneret to send the information.
  • Mike GMike G Posts: 2,702
    edited 2011-07-17 10:14
    @worthyamherst, I can't help you unless you post your script. How in the world would I know what data to send to the script?
Sign In or Register to comment.