Nodejs a solution to a socket hang up error when sending a POST request

Source: Internet
Author: User
Tags http post

Refer to the NODEJS website to send an HTTP POST request method, the implementation of a simulated post submission function. The actual use of the Times socket hang up error.

Later found to be the problem with the request header settings, the sending option needs to add Headers field information (this estimate is also related to the other side of the server, for the unfinished POST request header, may be discarded).

The complete code is as follows (students who encounter type problems can do a reference):

varQueryString = Require (' querystring ')), HTTP= Require (' http ');vardata =querystring.stringify ({info:' Hi ', test:5});varopt ={hostname:' Www.test.com ', Port:9094, Path:'/perationsqlquery ', Method:' POST ', headers: {' Content-type ': ' application/x-www-form-urlencoded ',    ' Content-length ': Data.length}};varreq = Http.request (OPT,function(res) {Res.on (' Data ',function(data) {Console.log (data.tostring ()); });}); Req.on (' Error ',function(e) {Console.log (' Problem with request: ' +e.message);}); Req.write (data); Req.end ();

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.