Issues With POST


(Steve Bratt) #1

Ive written a go script that sends a post request to a projector to log in and turn on my projector. its fairly simple as seen below, the first part logs in the second part turns on. this works fine from a lan connected device (chip)

package main

import (
“bytes”
“fmt”
“net/http”
“io/ioutil”
“regexp”
)

func main() {

    url := "http://192.168.0.50/cgi-bin/login_admin.cgi?lang=0"

    req, _ := http.NewRequest("POST", url, bytes.NewBuffer([]byte(`user=admin&password=&Login=login`)))

    req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
    req.Header.Set("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8")
    req.Header.Set("Accept-Encoding", "gzip, deflate")
    req.Header.Set("Accept-Language", "en-US,en;q=0.8")
    req.Header.Set("Cache-Control", "max-age=0")
    req.Header.Set("Connection", "keep-alive")
    req.Header.Set("Content-Length", "31")
    req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
    req.Header.Set("Host", "192.168.0.50")
    req.Header.Set("Origin", "http://192.168.0.50")
    req.Header.Set("Referer", "http://192.168.0.50/cgi-bin/login_admin.cgi?lang=0")
    req.Header.Set("Upgrade-Insecure-Requests", "1")
    req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36")

    client := &http.Client{}
    res, _ := client.Do(req)

    defer res.Body.Close()
    body, _ := ioutil.ReadAll(res.Body)

    fmt.Println(res)
    b := string(body)

    fmt.Println(b)

    r := regexp.MustCompile(`lang=0&([^&]+?)\"\>`)
    matches := r.FindStringSubmatch(b)

    fmt.Println(matches[1])


    req, _ = http.NewRequest("POST", "http://192.168.0.50/cgi-bin/projector.cgi?lang=0&" + matches[1],  bytes.NewBuffer([]byte(`btnPowerOn=Power ON`)))
    req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
    req.Header.Set("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8")
    req.Header.Set("Accept-Encoding", "gzip, deflate")
    req.Header.Set("Accept-Language", "en-US,en;q=0.8")
    req.Header.Set("Cache-Control", "max-age=0")
    req.Header.Set("Connection", "keep-alive")
    req.Header.Set("Content-Length", "31")
    req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
    req.Header.Set("Host", "192.168.0.50")
    req.Header.Set("Origin", "http://192.168.0.50")
    req.Header.Set("Referer", "http://192.168.0.50/cgi-bin/login_admin.cgi?lang=0")
    req.Header.Set("Upgrade-Insecure-Requests", "1")
    req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36")
    res, _ = client.Do(req)
    body, _ = ioutil.ReadAll(res.Body)
    c := string(body)
    fmt.Println(c)

}

im trying to send a similarly formatted command from smart things but i dont appear to be getting a response from the devicd, is the post command formatted correctly? im not getting any kind of response as if its not there (i was getting a response earlier with different code. the hex is in the network id and the device is selected in the hub

def on() {
log.debug("Executing hubAction on " + getHostAddress())

def uri = “/cgi-bin/login_admin.cgi?lang=0"
def body = [user:“admin”, password:”", Login:“login”]
def headers = [:]
headers.put(“HOST”, “192.168.0.50”)
headers.put(“Content-Type”, “application/x-www-form-urlencoded”)
headers.put(“Accept”, “text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,/;q=0.8”)
headers.put(“Accept-Encoding”, “gzip, deflate”)
headers.put(“Accept-Language”, “en-US,en;q=0.8”)
headers.put(“Cache-Control”, “max-age=0”)
headers.put(“Connection”, “keep-alive”)
headers.put(“Content-Length”, “31”)
headers.put(“Content-Type”, “application/x-www-form-urlencoded”)
headers.put(“Origin”, “http://192.168.0.50”)
headers.put(“Referer”, “http://192.168.0.50/cgi-bin/login_admin.cgi?lang=0”)
headers.put(“Upgrade-Insecure-Requests”, “1”)
headers.put(“User-Agent”, “Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36”)
log.debug “Headers are ${headers}”

def hubAction = new physicalgraph.device.HubAction(
method: “POST”,
path: uri,
headers: headers,
body: body
)
log.debug hubAction
hubAction
def result = sendHubCommand(hubaction)
}

im trying to parse a response with:

def parse(description) {
log.debug (“Parsing ‘${description}’”)
def msg = parseLanMessage(description)
log.debug “${msg}”
}

thanks


(Steve Bratt) #2

the logs show the following:

debug POST /cgi-bin/login_admin.cgi?lang=0 HTTP/1.1
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,/;q=0.8
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36
HOST: 192.168.0.50
Content-Type: application/x-www-form-urlencoded
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.8
Cache-Control: max-age=0
Connection: keep-alive
Content-Length: 31
Origin: http://192.168.0.50
Referer: http://192.168.0.50/cgi-bin/login_admin.cgi?lang=0
Upgrade-Insecure-Requests: 1

user=admin&password=null&Login=login

and

debug Headers are [HOST:192.168.0.50, Content-Type:application/x-www-form-urlencoded, Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,/;q=0.8, Accept-Encoding:gzip, deflate, Accept-Language:en-US,en;q=0.8, Cache-Control:max-age=0, Connection:keep-alive, Content-Length:31, Origin:http://192.168.0.50, Referer:http://192.168.0.50/cgi-bin/login_admin.cgi?lang=0, Upgrade-Insecure-Requests:1, User-Agent:Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36]

and

debug Executing hubAction on 192.168.0.50:80