简体   繁体   English

POST 到 request-promise-native 没有表单数据

[英]POST to request-promise-native has no formdata

This is probably something stupid I'm overlooking, but I am trying to use request-promise-native to POST to a form, and the response I get looks like the form data I sent is blank.这可能是我忽略的一些愚蠢的事情,但我正在尝试使用request-promise-native来发布表单,而我得到的响应看起来像我发送的表单数据是空白的。

Example : This works with the standard request :示例:这适用于标准request

const request = require('request');
request.post({
    url:'http://tycho.usno.navy.mil/cgi-bin/sidereal2-post.sh',
    form: {
        city: 'Santa Cruz',
        state: 'CA'
}
},
    function(err,httpResponse,body){
        if (err) {
         return console.log("Error: " + err);
        }
     console.log("got: " + body);
 });

Correctly produces:正确产生:

<html>
<title>Local Apparent Sideral Time </title>
<body>
<center>
<HR><h1>Local Apparent Sidereal Time</h1><HR>
<TABLE BORDER=4 ALIGN=CENTER VALIGN=CENTER CELLPADDING=5>
<TR><TH> SANTA CRUZ              , CA</TH>
<TR><TH> Longitude -122.04 degrees </TH>
<TR><TD ALIGN=CENTER><H1> 02:56 LST </H1></TD>
</TABLE>
</center>
</body>
</html>

But this code但是这段代码

const rp = require('request-promise-native');

const options = {
    method: 'POST',
    uri: 'http://tycho.usno.navy.mil/cgi-bin/sidereal2-post.sh',
    formData: {
        city: 'Santa Cruz',
        state: 'CA'
    },
};

rp(options)
    .then(function (body) {
        // POST succeeded
        console.log("got: "
        + body);
    })
    .catch(function (err) {
        // POST failed...
        console.log("ERROR: " + err);
    });

produces this result as if it didn't get the formData:产生这个结果,就好像它没有得到 formData:

<html>
<title>Local Apparent Sideral Time </title>
<body>
<center>
<HR><h1>Local Apparent Sidereal Time</h1><HR>
<HR><h1>Coordinates for                         , FO not found</h1><HR>
</center>
</body>
</html>

Am I forgetting brackets or punctuation or something?我是否忘记了括号或标点符号之类的? This seems to me to be identical to the example in the request-promise documentation .在我看来,这与request-promise 文档中的示例相同。

Maybe this will work for you也许这对你有用

    const rp = require('request-promise-native');

    const fetchData = async () => {
       const response = await rp.post('http://tycho.usno.navy.mil/cgi-bin/sidereal2-post.sh', {
           form: {
              city: 'Santa Cruz',
              state: 'CA'
           }
        });
     };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM