You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Google API Gateway(Beta版)含下划线请求头报400错误的解决问询

Solution for Underscore Request Headers in Beta Google API Gateway

I’ve run into similar header handling quirks with Google’s managed services before, so here are two practical workarounds that don’t require modifying your legacy clients or touching the API Gateway’s underlying server config:

Option 1: Use a Cloud Function as a Header-Transforming Proxy

This is a lightweight code-based approach that acts as a middleman to rewrite your headers before they hit the API Gateway.

  • Create an HTTP-triggered Cloud Function in your GCP project
  • In the function code, iterate over incoming request headers, replace all underscores (_) with hyphens (-) in the header keys
  • Forward the modified request to your API Gateway endpoint, then pass the gateway’s response back to the client

Here’s a quick Node.js example to get you started:

const axios = require('axios');

exports.transformRequestHeaders = async (req, res) => {
  // Rewrite headers: underscores to hyphens
  const transformedHeaders = {};
  Object.entries(req.headers).forEach(([key, value]) => {
    transformedHeaders[key.replace(/_/g, '-')] = value;
  });

  try {
    const gatewayEndpoint = 'https://your-api-gateway-url.example.com';
    const proxyResponse = await axios({
      method: req.method,
      url: `${gatewayEndpoint}${req.url}`,
      headers: transformedHeaders,
      data: req.body,
      responseType: 'stream'
    });

    // Relay the gateway's response to the client
    res.status(proxyResponse.status);
    proxyResponse.data.pipe(res);
  } catch (err) {
    res.status(err.response?.status || 500).send(err.message);
  }
};

Once deployed, update all your clients to point to the Cloud Function’s URL instead of the API Gateway directly.

Option 2: Cloud Load Balancer with Header Transformation Rules

If you prefer a no-code approach, use GCP’s Load Balancer to handle header rewriting at the edge.

  • Set up an HTTP(S) Load Balancer with your API Gateway as the backend service
  • Navigate to the load balancer’s routing rules and add a Modify Request Headers action
  • Configure a rule to match any request header with underscores, then rewrite the header key by replacing _ with -
  • Point your clients to the load balancer’s public IP or custom domain

This approach is great for high-traffic scenarios, as it leverages GCP’s managed edge infrastructure without running custom code.

Key Notes

  • For the Cloud Function route, adjust the timeout and concurrency settings in the GCP Console to match your traffic volume
  • For the Load Balancer, ensure your SSL certificate is properly configured if using HTTPS, and test all header transformations to avoid breaking standard headers like Content-Type
  • Both methods should preserve the original header values—only the keys are modified

内容的提问来源于stack exchange,提问作者Anand Mattikopp

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.11 07:36:57