Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9245477
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T09:16:33+00:00 2026-06-18T09:16:33+00:00

I’ve got such a code in node.js: var requestData = JSON.stringify({ id : data[‘user_id’]

  • 0

I’ve got such a code in node.js:

var requestData = JSON.stringify({ id : data['user_id'] });
var options = {
    hostname: 'localhost',
    port: 80,
    path: '/mypath/index.php',
    method: 'POST',
    headers: {
        "Content-Type": "application/json",
        'Content-Length': requestData.length
    }
};

var req = http.request(options, function(res) {
    console.log('STATUS: ' + res.statusCode);
    console.log('HEADERS: ' + JSON.stringify(res.headers));
    res.setEncoding('utf8');
    res.on('data', function (chunk) {
        console.log('BODY: ' + chunk);
    });
});

req.on('error', function(e) {
    console.log('problem with request: ' + e.message);
});

// write data to request body
req.write(requestData); 
req.end();

and PHP code:

<?php
    $data = $_POST;
    define('DS', '/');
    umask(000);
    file_put_contents(dirname( __FILE__ ).DS.'log.txt', json_encode($data), FILE_APPEND);
    echo json_encode($data);
?>

quite simple… but after making node.js POST request – PHP is not obtaining any data. I’ve tried many other ways of making that POST message to PHP but nothing works for me. I mean, $_POST is always empty.

Tried also request nodejs library:

    request.post({
        uri : config.server.protocol + '://localhost/someurl/index.php',
        json : JSON.stringify({ id : data['user_id'] }),
        },
        function (error, response, body) {
        if (!error && response.statusCode == 200) {
            console.log('returned BODY:', body);
        }
        def.resolve(function() {
            callback(error);
        });
    });

There should be really simple solution for my problem but I can’t find one.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T09:16:34+00:00Added an answer on June 18, 2026 at 9:16 am

    The $_POST array is only populated with HTML form POST submits. To emulate such a form submit, you need to:

    • Set the request Content-Type header exactly to application/x-www-form-urlencoded
    • Use the form encoding in the request body… I.E key=value&key2=value2 with percent-encoding where necessary.
    • Calculate the Content-Length header’s value exactly to the length of bytes that are being sent. You can only do this after having the fully encoded string, though byte conversion is not necessary in order to calculate the Content-Length since 1 character = 1 byte in urlencoded string.

    However, with your current code (provided all you have is ASCII), you can also do this:

    <?php
        $data = json_decode(file_get_contents("php://input"));
        $error = json_last_error();
        if( $error !== JSON_ERROR_NONE ) {
            die( "Malformed JSON: " . $error );
        }
    
        define('DS', '/');
        umask(000);
        file_put_contents(dirname( __FILE__ ).DS.'log.txt', json_encode($data), FILE_APPEND);
        echo json_encode($data);
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using jsonparser to parse data and images obtained from json response. When
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am confused How to use looping for Json response Array in another Array.
I am using JSon response to parse title,date content and thumbnail images and place
I've got a string that has curly quotes in it. I'd like to replace
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
i got an object with contents of html markup in it, for example: string
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.