【问题标题】:override the content-type being set in the req.header with node js?用节点js覆盖req.header中设置的内容类型?
【发布时间】:2020-03-11 05:04:36
【问题描述】:

我有一个 Web 服务向 API 发出发布请求,并且在此过程中,由于某种原因,标头的内容类型值被从“application/json”覆盖到“text/html”。这导致 POST 请求失败,因为 API 只接受内容类型:'application/json'。为了克服这个问题,我打算让 Web 服务接触一个代理 Web 服务器,该服务器将实现服务器端代码以将 req.header 值“content-type”修改回“application/json”并发送 post 请求API 的 req.body 和 req.headers。我正在尝试在节点 js 中执行此操作(使用 express js)。如何覆盖代理节点 js 服务器上的 req.header?我曾尝试使用仅应用程序/json 的接受内容类型,但这并没有满足我的需要:(

【问题讨论】:

    标签: node.js express http post content-type


    【解决方案1】:
    let request = require('request');
    
    let proxyRequest = (data,headers)=>{
    var options = {
      'method': 'POST',
      'url': 'your-api-url',
      if (headers.hasOwnProperty('Content-Type')){
           delete headers['Content-Type']
        }
     
        headers['Content-Type']= 'application/json'
    
      body: data
    
    };
        request(options, function (error, response) { 
          if (error) throw new Error(error);
          console.log(response.body);
        });
    
    
    };
    

    【讨论】:

      猜你喜欢
      • 2013-02-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-22
      • 1970-01-01
      • 2021-08-09
      • 2021-05-03
      相关资源
      最近更新 更多