0

i want to pass data with GET method in node js , my response is ok but in php file $_GET is empty. what is wrong?

var querystring = require('querystring');
var req = require('request');
var form = {
    number: 'test',
    msg: 'test',
};

var formData = querystring.stringify(form);
var contentLength = formData.length;

req({

    headers: {
      'Content-Length': contentLength,
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    uri: 'http://localhost:8080/sms/index.php',
    body: formData,
    method: 'GET'

  }, function (err, res, body) {
        console.log('res is',res);
        console.log('err',err);
  });

1 Answer 1

3

An HTTP GET request cannot have a request body. $_GET parses parameters from the query string. The headers Content-Lengthand Content-Type do not make sense for a GET request as they apply to the body of the request, which GET cannot have. You need to use the qs option instead of body.

req({
    uri: 'http://localhost:8080/sms/index.php',
    qs: form,
    method: 'GET'
  }, function (err, res, body) {
        //
  });
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.