http请求头 Content-Type 详解(三)

摘要:用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" : 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"}
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值