Customize HTTP IN response

Is there a way to customize the response for a POST request to HTTP IN?

Right now I’m only getting an OK, but I’d like to get a different response if, for example, my impee is offline.

This functionality is available with agents (in beta right now) and the http.onrequest function.

If you like, I can set you up on our beta, and you can give it a try :slight_smile:

Edit: included link to devwiki page.

on the forum I found an example for use with Agents but I can’t find it anymore, so here is my version. You have to create you web app yourself but for testing I go to http://hackst.com/

Agent:

`server.log("access me via "+http.agenturl());

local YellowState = 0;
local RedState = 0;

device.on(“setRed”, function(data) {
RedState = data;
});

device.on(“setYellow”, function(data) {
YellowState = data;
});

http.onrequest(function(request, response) {
server.log("http.onrequest received ");
local code = null;
local message = null;
server.log("request.path: " + request.path);
if (request.path == “/lights”){

    if (request.method == "GET"){
        code = 200;
        message = "{ 'Red': " + RedState + ", 'Yellow': " + YellowState + " }";
    }
else if (request.method == "POST"){
        try {
            local data = http.jsondecode(request.body);
            if ("Red" in data){
                //server.log("Red: " + data.Red);
                device.send("setRed", data.Red);
            }
            if("Yellow" in data){
                //server.log("Yellow: " + data.Yellow);
                device.send("setYellow", data.Yellow);
            }
            code = 200;
            message = "OK";
        }
        catch (e) {
            code = 501;
            message = "Unacceptable";
            server.log(e);
        }
    }}
else{code = 404; message = "Not Found";}
response.send(code, message);

});`

Device:

`
class LEDController{
pin = null;
colour = null
state = 0;

constructor(ledPin, ledColour){
    pin = ledPin;
    colour = ledColour;
    pin.configure(DIGITAL_OUT);
}

function TurnOn(){
    Write(0);
}

function TurnOff(){
    Write(1);
}

function Write(newState){
    server.log("Write called");
    if (newState == 0 || newState == 1)
    {
        state = newState;
        server.log(colour + ": " + newState);
        agent.send("set" + colour, newState);
        pin.write(newState);
    }
    else
    {
        server.log(newState + " is not a valid LED state. Please use 0 or 1.");
    }
    
}

};
local RedLED = LEDController(hardware.pin8, “Red”);
local GreenLED = LEDController(hardware.pin9, “Yellow”);
//RedLED.TurnOff();
//GreenLED.TurnOff();

agent.on(“setYellow”, function(state) {
GreenLED.Write(state);
});
agent.on(“setRed”, function(state) {
RedLED.Write(state);
});
imp.configure(“Red Light Yellow Light”,[],[]);`

In this example I would not use HTTP IN’s URL ?
I would use instead http.agenturl() to post?

Correct - you would use the url that http.agenturl() returns.

You can also find the agent URL by clicking on “Device Settings.”

I would like to use http.onrequest function in my imp.
Connect this function, please

send @beardedinventor a PM

I see my imp dont connection with site!!! I cant download device program

@sunhunter - could you please send an email to info@electricimp.com describing your problem.