http请求头 Content-Type 详解(三)
2018-02-24 15:33
417 查看
摘要:用NodeJs request模块模拟第一篇用html请求的http请求
或者这样写:
http请求:
注意请求体内容做了url_encode
http请求:
post文件,代码:
http请求:
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" : 123 });
或者这样写:
request.post({ url : url, proxy:'http://localhost:8888', form : {"name" : "zhangtao","address" : 123} }, 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=123&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([1, 2, 3]) }; 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([1, 2, 3]), "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 5 Z= dsz*mU Ԫ bpRK \ [k Ib# q h_: Q qF)83 7A ? S M V _ & E7 ١ [ D r~M* ~H B @@ Xk $ )g@- Kb i Z" j a]GY 5 L mƞ6 5/ qI! - Lc ~ V#O " F* 8 } >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油"@ ] µH@km " 3I 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 [ " {7 ?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"}
相关文章推荐
- http请求头 Content-Type 详解(二)
- http请求头 Content-Type 详解(一)
- 【META http-equiv="Content-Type" Content="text/html; Charset=*】意义详解
- Content-Type详解
- Content-type 的说明 ,即 HTTP请求头的类型有哪些?
- response.setContentType()的作用及MIME参数详解
- Content-Type详解
- 网页直接打开PDF或强制下载以及header和content-type参数详解
- response.setContentType()的作用及MIME参数详解
- [转]Content-type 的说明 ,即 HTTP请求头的类型有哪些?
- response.setContentType() 详解
- Jquery Post提交时Content-Type的不同取值详解
- Content-type 的说明 ,即 HTTP请求头的类型有哪些?
- Content-type的说明即HTTP请求头的类型整理
- http请求中的Content-Type,详解
- response.setContentType()的作用及MIME参数详解
- Content-type 的说明 ,即 HTTP请求头的类型有哪些
- 详解http请求中的Content-Type
- JavaWeb学习篇之----容器Response详解(有关response setCharacterEncoding和setContentType)详细对比说明
- 关于Content-Type的http请求头的格式