如何在Node.js中发送单部分POST请求?

时间:2014-03-03 08:01:12

标签: node.js httprequest phantomjs

PhantomJs的网络服务器不支持多部分请求,因此我尝试从NodeJ发送单部分请求。

不幸的是,nodejs示例看起来是多部分的。用NodeJs有什么办法吗?

http://nodejs.org/api/http.html#http_http_request_options_callback

编辑: 在nodejs文档中它提到: 发送“Content-length”标头将禁用默认的chunked编码。 但不幸的是,它仍然是多部分,而不是多部分:P

edit2:为了显示代码,显示一个简单的例子有点困难,但是这里有:

node.js代码(它的Typescript代码):

```

    //send our POST body (our clientRequest)
    var postBody = "hello";

    var options : __node_d_ts.IRequestOptions = {
        host: host,
        port: port,
        path: "/",
        method: "POST",
        headers: {
            "Content-Type": "application/json",
            "Content-length": postBody.length
        }
    };


    //logger.assert(false);
    var clientRequest = http.request(options,(response: http.ServerResponse) => {
        //callback stuff here
    });

    clientRequest.on("error", (err) => {
        thisObj.abort("error", "error,request error", err);
    });

    //clientRequest.write();
    clientRequest.end(postBody);

```

当我从PhantomJS读取结果时,post / postRaw字段为空。 当我使用像Chrome“高级REST客户端”扩展程序这样的工具发送POST主体时,phantomjs就没问题了。

我没有网络嗅探器,但如此处所述,它表示phantomjs不适用于多部分因此我认为这是一个很好的猜测:How can I send POST data to a phantomjs script

EDIT3:

的确,这是我的chrome扩展程序中的phantomjs请求(有效帖子) //cookie, userAgent, and Origin headers removed for brevity {"headers":{"Accept":"*/*","Accept-Encoding":"gzip,deflate,sdch","Accept-Language":"en-US,en;q=0.8,ko;q=0.6","Connection":"keep-alive","Content-Length":"5","Content-Type":"application/json","DNT":"1","Host":"localhost:41338", "httpVersion":"1.1","method":"POST","post":"hello","url":"/"}

这里的请求phantomjs来自上面显示的nodejs代码:

//full request, nothing omitted! {"headers":{"Connection":"keep-alive","Content-Type":"application/json","Content-length":"5","Host":"10.0.10.15:41338"},"httpVersion":"1.1","method":"POST","url":"/"}

0 个答案:

没有答案