微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

node.js – 如何使用node-curl发布数据?

我对使用node.js的LINUX很新.这只是我的第二天.我使用 node-curl进行卷曲请求.在下面的链接中,我找到了Get请求的示例.任何人都可以使用node-curl为我提供Post请求示例.

https://github.com/jiangmiao/node-curl/blob/master/examples/low-level.js

解决方法

您需要使用setopt来指定cURL请求的POST选项.您应首先考虑的选项是CURLOPT_POST和CURLOPT_POSTFIELDS.从节点卷曲链接libcurl documentation

CURLOPT_POST

A parameter set to 1 tells the library to do a regular HTTP post. This will also make the library use a “Content-Type: application/x-www-form-urlencoded” header. (This is by far the most commonly used POST method).

Use one of CURLOPT_POSTFIELDS or CURLOPT_copYPOSTFIELDS options to specify what data to post and CURLOPT_POSTFIELDSIZE or CURLOPT_POSTFIELDSIZE_LARGE to set the data size.

Optionally,you can provide data to POST using the CURLOPT_READFUNCTION and CURLOPT_READDATA options but then you must make sure to not set CURLOPT_POSTFIELDS to anything but NULL. When providing data with a callback,you must transmit it using chunked transfer-encoding or you must set the size of the data with the CURLOPT_POSTFIELDSIZE or CURLOPT_POSTFIELDSIZE_LARGE option. To enable chunked encoding,you simply pass in the appropriate transfer-encoding header,see the post-callback.c example.

CURLOPT_POSTFIELDS

… [this] should be the full data to post in a HTTP POST operation. You must make sure that the data is formatted the way you want the server to receive it. libcurl will not convert or encode it for you. Most web servers will assume this data to be url-encoded.

This POST is a normal application/x-www-form-urlencoded kind (and libcurl will set that Content-Type by default when this option is used),which is the most commonly used one by HTML forms. See also the CURLOPT_POST. Using CURLOPT_POSTFIELDS implies CURLOPT_POST.

If you want to do a zero-byte POST,you need to set CURLOPT_POSTFIELDSIZE explicitly to zero,as simply setting CURLOPT_POSTFIELDS to NULL or “” just effectively disables the sending of the specified string. libcurl will instead assume that you’ll send the POST data using the read callback!

有了这些信息,您应该能够将以下选项添加到低级示例中,以使其发出POST请求:

var fieldsstr = '{}';
curl.setopt('CURLOPT_POST',1); // true?
curl.setopt('CURLOPT_POSTFIELDS',fieldsstr);

您需要调整fieldsstr的内容以匹配服务器所期望的格式.根据文档,您可能还需要对数据进行url编码 – 这应该像根据this post使用encodeURIComponent一样简单.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐