Http In Response

Hello

I’m sending an http request (Http In) to my device, and it works fine.

I won’t, to send a Response when I receive the data, but I couldn’t get it to work.

Below is my Http In code.

Watt should I add to the code for sending replay to the client.

class cyclesInput extends InputPort{
type = "float"
name = "message"

function set(seconds) {
    local d = seconds.x;
    server.log("seconds.x: "+d+" seconds.y: "+seconds.y);
}

}

imp.configure(“Hello World Program”, [cyclesInput()], );

Thanks’ allot
Avi

Unfortunately, you can’t send a response using the HTTP In node.

This is one of the problems we’ll be solving with our new IDE and agents (coming soon). The documentation for http.onrequest walks through an example of processing an incoming HTTP request, and sending a response.

You can use an output port to do a http POST but this will not technically be in response to your input - just a coincidental thing that happens.

You would set up an input port - check the examples and you would need some code server-side to handle the POST.

I am also very new to this and don’t quite have it all figured out but there are examples somewhere in the wiki.

I don’t know about waiting for agents… as they have been ‘coming soon’ for quite a long time now.

Using OutputPorts and HTTP Request nodes has a couple serious downsides:

  1. The request URL is hardcoded, and can’t be set programmatically - this means you need to know exactly where the request (“response”) is going, and it can only really go to a single place.
  2. As mjkuwp94 mentions: this is not a true response, “just a coincidental thing that happens.” This means that you can’t use return codes, and your client needs to be a server that can handle HTTP requests (in other words, it can’t be a webpage, mobile application, etc).

All that said, I’ll work up an example of how to hook all of this together and post it shortly.

You’re right: a public release of the new IDE + agents have been ‘coming soon’ for quite a while now, and unfortunately we don’t have a concrete release date yet - but if you’re interested in participating in the beta, let us know and we’ll get you access.

I’ve worked up a generic example to process an incoming HTTP request (through a class that extends InputPort) and send a ‘response’ to a specified location (through an OutputPort and an HTTP Request node). To make this code do something more interesting with the incoming request, simply change what happens in SimpleRequestHandler, or create your own request handler.

Right now I’m just sending the “response” to a requestb.in - this is a really simply way to see what your HTTP Requests look like, and make sure they look correct. In a real world example you would be sending a request back to your server and doing something with it there.

If anyone has questions about the example, let me know.

`// This is the function that actually handles the incoming HTTP Request
function SimpleRequestHandler(data) {
server.log(“Got a request with value=’” + data + “’”);
return 200;
}

// handles incoming HttpIn requests, and sends “responses” to an HttpRequest node through
// an output port. The ResponsePort sends a url-encoded message where “value” is the response code.
class HttpInPortWithResponse extends InputPort {
RequestHandler = null; // function that will be called whenever a request comes into the input port. Should return a status code.
ResponsePort = null; // OutputPort that will have it’s value set based on RequestHandlers response.

constructor(requestHandler, portName, responsePort) {
    base.constructor(portName);         // Set the port name so it's easy to identify in the planner
    OutputPort = responsePort;
    RequestHandler = requestHandler;
}

function set(value) {
    local responseCode = RequestHandler(value); // Run the request handler
    OutputPort.set(responseCode);               // and return the result
    server.log("Responded with " + responseCode);
}

}

local httpOutPort = OutputPort(“HTTPResponse”);
local httpInPort = HttpInPortWithResponse(SimpleRequestHandler, “HTTPRequest”, httpOutPort);

imp.configure(“Http Example”, [httpInPort], [httpOutPort]);`

Here’s what the planner looks like:

Thanks a lot.

I think I would like to use the beta version (the function: http.onrequest).

How can I join the beta users?

Thanks Avi