zoukankan      html  css  js  c++  java
  • 搭建代理服务器时的笔记,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参数传递不全,写长了可能会报错

  • 相关阅读:
    python3----练习题(斐波那契)
    python3----运算符
    python3----函数、匿名函数
    python3----生成器generator(yield)
    Python捕获异常
    OS模块
    发送邮件
    IO文件读写
    Unittest框架概念
    生成报告
  • 原文地址:https://www.cnblogs.com/liujinyu/p/10001940.html
Copyright © 2011-2022 走看看