Electron api integration with a website


#1

Hi,

I am new to electron and building an App for my website. I want to send post or get request with parameters to my website and get back response in the form of json.

I can’t figure out how to send data with my request, please have a look on the following code:

options = {
method: ‘POST’,
protocol: ‘http:’,
hostname: ‘matthewfermin.staging.wpengine.com’,
path: ‘/api/login’
}

var results = '';

const request = net.request(options)
request.on('response', (response) => {
	console.log(`STATUS: ${response.statusCode}`)
	console.log(`HEADERS: ${JSON.stringify(response.headers)}`)
	// alert(`HEADERS: ${JSON.stringify(response.headers)}`);
	// win.webContents.executeJavaScript("alert('`BODY: ${chunk}`');");
	response.on('data', (chunk) => {
		console.log(`BODY: ${chunk}`)
		win.webContents.executeJavaScript("console.log('`BODY: ${chunk}`');");
	})
	response.on('end', () => {
		console.log('No more data in response.')
		win.webContents.executeJavaScript("console.log('No more data in response.');");
	})
})
request.end()

I appreciate your response. Thanks.


#2

When interacting with the Node functionality in Electron, you’re virtually always going to be able to perform actions just like you would with any other Node program. Here’s a Stack Overflow question about POST requests with data.


#3

Thanks @DamnedScholar.
But I can’t figure out how to send params alongwith POST method. i.e. I want to send param1 and param2 with POST request and receiver those params on server side.

Help is greatly appreciated.


#4

That’s what the post_data and req.write(post_data) accomplish. This is also covered in the Node docs. What are you having difficulty with, specifically?