Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

net.request post data not working

Posting data using net.request is not working. It's reaching the URL. But data not posted. My code is below. Please help me on this.

const net = remote.net;
const querystring = require('querystring');

//**
var postData = querystring.stringify({
    'username' : 'test',
    'password': 'test'
});

const request = net.request({
  method: 'POST',
  url: 'http://127.0.0.1/post.php',
});

request.on('error', (error) => {});

request.on('response', (response) => {});


request.write(postData);
request.end();
like image 759
Jay Avatar asked Oct 18 '22 11:10

Jay


1 Answers

I know it's been a while. But for the next people who will have the same problem.

Don't forget you must be declare the size of your "postData" in the header. for example :

var postData = JSON.stringify({"q" : sqlQuery });
const request = net.request({
        method: 'POST',
        protocol: 'http:',
        hostname: '127.0.0.1',
        port: 3000,
        path: '/select',
        headers: {
          'Content-Type': 'application/json',
          'Content-Length': postData.length
        }
       }) 
request.on('response', (response) => {
.... // Something
})
request.write(postData)
request.end()
like image 166
Iwoks Avatar answered Oct 21 '22 07:10

Iwoks