搭建代理服务器时的笔记,request使用笔记

request 请求笔记:

1、opation中使用form字段传参 对应 content-type': 'application/x-www-form-urlencoded',如果想要content-type设置为

var options = {
  url: "http://localhost:8888/getjs",
  method:'POST',
  form:{
    limit: 20,
    offset: 0,
    orderId:'',
    receiverPhone:'',
    skuId:'',
    skuName:''
  }
}; 
request(options, (error, response, body)=>{
  // console.log(" response:===",response);
    console.log(" ============body begin============");
    console.log(body);
    console.log(" ============body end============");
    console.log("[response.headers]:",response.headers)
  if (!error && response.statusCode == 200) {
    // var info = JSON.parse(body);
  }
});

注意必须设置的几个字段  json 、headers中的content-type 还有不能使用form字段  必须用body

var options = {
  url: localurl_localhost,
  method:'POST',
  json:true,//这个字段必须
  headers:{
    'content-type':'application/json', //这个字段必须
  },
  body:{ //想要发起 content-type为application/json的请求  就要用body字段传参
    limit: 20,
    offset: 0,
    orderId:'',
    receiverPhone:'',
    skuId:'',
    skuName:''
  }
};

 2、注意,Content-Length最好不填,不填的话,require会默认计算长度并填入,反而更安全,因为如果填写有误的话会直接影响返回值,写小了,body参数传递不全,写长了可能会报错

转载于:https://www.cnblogs.com/liujinyu/p/10001940.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值