Home AMX User Forum NetLinx Studio

HTTP Library

I was wondering if there is any HTTP libraries made for AMX to do simple HTTP/REST/XML requests (i.e Posts). As i am unsure on how to establish a port as the Netlinx Help is not that helpful.

Comments

  • This is my code right now which is to open a TCP Socket. But once I open the TCP socket I don't know how to send a POST via the TCP.
    PROGRAM_NAME='TCPComm'
    
    DEFINE_CONSTANT
    INCLUDE 'SNAPI'
    
    DEFINE_VARIABLE
    CHAR strIpAddress[16] = '192.168.1.125';
    INTEGER nIpPort = 80 ;
    INTEGER nIpConneted = 0; // 0 = disconnected, 1 = connected.
    CHAR strIpStack[50];
    
    DEFINE_FUNCTION PopStack(){
    	IF(LENGTH_STRING(strIpStack))
    		SEND_STRING dvIP, strIpStack
    	strIpStack = '';
    }
    
    DEFINE_FUNCTION PushStack(CHAR sCmd[]){
    	strIpStack = sCmd;
    }
    
    DEFINE_FUNCTION ConnectIP(){
    	IP_CLIENT_OPEN(dvIp.PORT, strIpAddress, nIpPort, IP_TCP); // this is the main connection command
    }
    
    DEFINE_FUNCTION SendCommand(CHAR sCmd[]){
    	IF(nIpConneted)
    		SEND_STRING dvIP, sCmd
    	ELSE {
    		PushStack(sCmd);
    		ConnectIP();
    	}
    }
    
    DEFINE_EVENT
    //INSERT COMMANDS HERE
    
    DATA_EVENT[dvIP] {
    	STRING: {
    	}
      ONLINE :{
    		nIpConneted = 1;
    		PopStack();
    	}
      OFFLINE:{
    		nIpConneted = 0;
    		ConnectIp();		
    	}
      ONERROR : {
    		nIpConneted = 0;
      }
    }
    
    
  • DHawthorneDHawthorne Posts: 4,584
    Sending a POST is just sending the string to your socket once it's open, terminated with two CRLF pairs. Unfortunately, different web entities are going to require different formats for the header and data inside the POST, and you are going to need to know exactly what they require before it will work. I have seen some RSS feeds, for example, that ignored any requests that didn't include a user-agent in the header. That part you are going to have to find out from the site you are accessing. Make certain, as well, that POST is what is needed, and not GET, some implementations are kind of murky on that. The intended use is clear enough, but not every site developer gets all that stuff straight.
  • jweatherjweather Posts: 320
    You'll need to know what content-type your device is expecting. If you're replicating the posting of a web form, that will typically be x-www-form-urlencoded (and=content+will&look=like+this), but your best bet is to use the Developer Tools/F12 function on your web browser and find out what's actually being sent by their web client (if there is one). As DH mentioned, you may need to add other headers to make the device happy. Post a link to the API reference and I can take a look if you like.
    SEND_STRING dvIp, "'POST / HTTP/1.1', $0D, $0A, 
    	'Content-Length: ', itoa(length_string(strIpStack)), $0D, $0A, 
    	'Content-Type: application/x-www-form-urlencoded', $0D, $0A, $0D, $0A,
    	strIpStack"
    

    Oh, and you'll want to close the connection after you get a response, and open a new one for the next command, unless you want to mess with HTTP/1.1 connection handling, which I've never paid attention to.
Sign In or Register to comment.