搭建代理服务器时的笔记,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