Post form to unix socket


#1

I’m trying to POST a form values to a unix socket but I need some help to get it working.

So far I’ve got an HTML form with a global object ‘Sk’ to send the data. Here’s the html:

<form id="thisForm" onsubmit='Sk.POSTForm("//skelectron/main", $(this).html());'>
    <textarea name="body" cols="30" rows="10"></textarea>
    <input type="submit" />
</form>

And here is the js function with the ‘this’ refering to the ‘Sk’ object:

this.POSTForm = function(addr, formHTML){
        var url = require('url')
        var URL = url.parse(addr, true, true)
        var req = http.request({
            hostname: URL.hostname,
            socketPath:__dirname+"/../../skelectron.sock",
            method : "POST",
            headers: {
               'Content-Type': 'application/x-www-form-urlencoded',
               'Content-Length': formHTML.length
                }
        });
        req.write(formHTML);
        req.end()
    }

This code connects to the socket fine but I am not receiving the form textarea value on the other end. Actually, I don’t think I am sending that value at all. All I get is the form inner HTML. But how to send the values?

Thanks in advance for sharing your web skills.


#2

It looks like $(this).html() is probably meant to get the innerHTML of an element. To get the values you can iterate over document.forms.thisForm.elements or whatever the $ alternative is.


#3

thanks, but anyway I try to use the DOM Form object I get an error:

Uncaught InvalidStateError: Failed to read the 'selectionStart' property from 'HTMLInputElement': The input element's type ('submit') does not support selection.

I tried replacing the $(this) with this, document.forms.thisForm.elements etc, but I keep on hitting that error and my POSTForm function is not reached.


#4

Try $(this).find('textarea[name="body"]').value() instead of $(this).html()


#5

yeah, that works.
I was looking for an easier/more global solution: in other forms with a lot of inputs, the value selection process would quickly become cumbersome.

Is there a way to redirect all browser-window requests to a socket? I’d love to be able to use the ‘action’ attribute of the form and have the form POST to my socket. Then on the server side of the socket, I would decide what to do with the requested URL.


#6

In that case use jQuery’s serializeArray method :slight_smile:

I don’t know the answer to your second question…


#7

Ok, but if I want to require from the POST method using a solution like: this.

Try using the provided documentation loadURL method

In the main process create a server with the http protocol, in the process renderer create an IPC event listener in such a way that with a button the form will open, and to listen to this evente the main process uses the custom .loadURL method so that the Window show my form from the HTTP protocol.

But it happens that in the application of Electron js nothing is shown, while in the web browser I can observe my form with http protocol.

I’m starting on Electron, it would help me a lot if you helped me with that, somewhat specified.