摘要:用NodeJs request子產品模拟第一篇用html請求的http請求
一、Content-Type: application/x-www-form-urlencoded
代碼:
var request = require("request");
var url = "http://localhost/test/curl/service.php";
request.post({
url:url,
proxy:'http://localhost:8888'
}, function(err, response, body){
console.log(response.headers);
console.log(body);
}).form({
"name" : "zhangtao",
"address" :
});
或者這樣寫:
request.post({
url : url,
proxy:'http://localhost:8888',
form : {"name" : "zhangtao","address" : }
}, function(err, response, body){
console.log(body);
});
http請求:
POST http://localhost/test/curl/service.php HTTP/1.1
host: localhost
content-type: application/x-www-form-urlencoded
content-length: 54
Connection: close
name=zhangtao&address=&encode=%E4%B8%AD%E6%96%87%26
注意請求體内容做了url_encode
二、content-type: multipart/form-data;
代碼:
var request = require("request");
var url = "http://localhost/test/curl/service.php";
var formData = {
"name" : "lu",
"node_buffer" : new Buffer([, , ])
};
request.post({
url : url,
proxy : 'http://localhost:8888',
formData : formData
}, (err, response, body) => {
console.log(body);
});
http請求:
POST http://localhost/test/curl/service.php HTTP/1.1
host: localhost
content-type: multipart/form-data; boundary=--------------------------159485793623903092889117
content-length: 314
Connection: close
----------------------------159485793623903092889117
Content-Disposition: form-data; name="name"
lu
----------------------------159485793623903092889117
Content-Disposition: form-data; name="node_buffer"
Content-Type: application/octet-stream
----------------------------159485793623903092889117--
(注意上面node_buffer裡面的值是二進制的,這裡複制到這裡不顯示了)
post檔案,代碼:
var request = require("request");
var fs = require("fs");
var url = "http://localhost/test/curl/service.php";
var formData = {
"name" : "lu",
"node_buffer" : new Buffer([, , ]),
"node_file" : fs.createReadStream(__dirname + '/share-qq.png')
};
request.post({
url : url,
proxy : 'http://localhost:8888',
formData : formData
}, (err, response, body) => {
console.log(body);
});
http請求:
POST http://localhost/test/curl/service.php HTTP/1.1
host: localhost
content-type: multipart/form-data; boundary=--------------------------521008285990597015283124
content-length: 1598
Connection: close
----------------------------521008285990597015283124
Content-Disposition: form-data; name="name"
lu
----------------------------521008285990597015283124
Content-Disposition: form-data; name="node_buffer"
Content-Type: application/octet-stream
----------------------------521008285990597015283124
Content-Disposition: form-data; name="node_file"; filename="share-qq.png"
Content-Type: image/png
PNG
IHDR V V b PLTEF M Њ s } m j U R r X y c ] O ߭ ߒ ڄ g Զ ߙ ܀ ޥ ޢ ݞ K ڼ X> ^IDATX ͙ V @F 鄠!V &q\ j":" G љ s ' j |
m Բ- C B j z Z I Z= dsz*mU Ԫ bpRK \
[k Ib# q h_: Q qF)83 7A ? S M V _ & E7 ١ [ D r~M* ~H B @@ Xk
$ )[email protected] Kb
i Z" j a]GY 5 L mƞ6 5/ qI! - Lc ~ V#O " F* } >iQ" %H
# T Z T m Ww Q %8 j ܻ s v * ' K;$ ю) ׆o + Y ˍ # ꑥ] U ^ F " \m TꑶI o3 j0 'B K8䚫 L ع J+ &L v ف` iq油"@ ] µ[email protected] " I K ej x xҡ- Q q{. j Ը"!eTj ܥ /n bR ��y 5 Y : o !E _; *
cx *Y @W aiRD ; n v 1k ^) tI 2;
A4 _ XE h Oc 0 + 쓯_ R ҕ ˩ nc }7 . ' \ =p c
| ɗф qC Bo o d v C V ۔v ҽ x X w e { Gp` k 9 @> ] A+ G [ " { ?g S J ?[ 㑂 h( b?~
; D Gԅ IEND B`
----------------------------521008285990597015283124--
三、Content-Type: application/json;
代碼:
var request = require("request");
var url = "http://localhost/test/curl/service.php";
var sendJson ={
a : "a",
b : "b"
}
request({
url: url,
method: "POST",
proxy : 'http://localhost:8888',
json: true,
headers: {
"content-type": "application/json"
},
body: sendJson
}, function(error, response, body) {
console.log(body);
});
http請求:
POST http://localhost/test/curl/service.php HTTP/1.1
content-type: application/json
host: localhost
accept: application/json
content-length: 17
Connection: close
{"a":"a","b":"b"}