[[["わかりやすい","easyToUnderstand","thumb-up"],["問題の解決に役立った","solvedMyProblem","thumb-up"],["その他","otherUp","thumb-up"]],[["わかりにくい","hardToUnderstand","thumb-down"],["情報またはサンプルコードが不正確","incorrectInformationOrSampleCode","thumb-down"],["必要な情報 / サンプルがない","missingTheInformationSamplesINeed","thumb-down"],["翻訳に関する問題","translationIssue","thumb-down"],["その他","otherDown","thumb-down"]],["最終更新日 2025-08-21 UTC。"],[[["\u003cp\u003eThis document focuses on handling multi-valued HTTP headers within Apigee and Apigee hybrid environments, highlighting how to access and manage headers with multiple values correctly.\u003c/p\u003e\n"],["\u003cp\u003eHTTP headers can contain multiple values separated by commas, and the order of these values is significant, like in the \u003ccode\u003eX-Forwarded-For\u003c/code\u003e header where the order of the IP addresses represents network hops.\u003c/p\u003e\n"],["\u003cp\u003eAccessing multi-valued headers in Apigee using methods that only return the first value can lead to incorrect processing and unexpected results, impacting both API proxy flow and backend services.\u003c/p\u003e\n"],["\u003cp\u003eThe recommended best practice is to use the \u003ccode\u003erequest.header.header_name.values.string\u003c/code\u003e flow variable to capture all values from a multi-valued header, or access individual values using \u003ccode\u003erequest.header.header_name.N\u003c/code\u003e, where N is the index of the value.\u003c/p\u003e\n"],["\u003cp\u003eAdditionaly, the \u003ccode\u003esubstring()\u003c/code\u003e method or Javascript \u003ccode\u003esplit()\u003c/code\u003e function can be used to correctly read multi-valued headers in an Apigee flow.\u003c/p\u003e\n"]]],[],null,["*You're viewing **Apigee** and **Apigee hybrid** documentation.\nView [Apigee Edge](https://docs.apigee.com/api-platform/antipatterns/multi-value-http-headers) documentation.*\n\nThe HTTP headers are the name value pairs that allow the client applications and backend services\nto pass additional information about requests and responses respectively. Some simple examples are:\n\n- Authorization request header passes the user credentials to the server: \n\n ```actionscript-3\n Authorization: Basic YWxhZGRpbjpvcGVuc2VzYW1l\n ```\n- The `Content-Type` header indicates the type of the request/response content being sent: \n\n ```text\n Content-Type: application/json\n ```\n\nThe HTTP Headers can have one or more values depending on the\n[header field definitions](https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html).\nA multi-valued header will have comma separated values. Here are a few examples of headers that contain multiple values:\n\n- `Cache-Control: no-cache, no-store, must-revalidate`\n- `Accept: text/html, application/xhtml+xml, application/xml;q=0.9, */*;q=0.8`\n- `X-Forwarded-For: 10.125.5.30, 10.125.9.125`\n\n| **Note:** The order of the multiple header values is important and must be preserved. For example with `X-Forwarded-For`, the IP addresses are listed in order of network hops from first to last.\n\nApigee allows the developers to access headers easily using\n[flow variables](/apigee/docs/api-platform/reference/variables-reference)\nin any of the policies or conditional flows. Here are the list of variables that can be used\nto access a specific request or response header in Apigee:\n\nFlow variables:\n\n- `message.header.`\u003cvar translate=\"no\"\u003eheader-name\u003c/var\u003e\n- `request.header.`\u003cvar translate=\"no\"\u003eheader-name\u003c/var\u003e\n- `response.header.`\u003cvar translate=\"no\"\u003eheader-name\u003c/var\u003e\n- `message.header.`\u003cvar translate=\"no\"\u003eheader-name\u003c/var\u003e`.`\u003cvar translate=\"no\"\u003eN\u003c/var\u003e\n- `request.header.`\u003cvar translate=\"no\"\u003eheader-name\u003c/var\u003e`.`\u003cvar translate=\"no\"\u003eN\u003c/var\u003e\n- `response.header.`\u003cvar translate=\"no\"\u003eheader-name\u003c/var\u003e`.`\u003cvar translate=\"no\"\u003eN\u003c/var\u003e\n\nJavascript objects:\n\n- `context.proxyRequest.headers.`\u003cvar translate=\"no\"\u003eheader-name\u003c/var\u003e\n- `context.targetRequest.headers.`\u003cvar translate=\"no\"\u003eheader-name\u003c/var\u003e\n- `context.proxyResponse.headers.`\u003cvar translate=\"no\"\u003eheader-name\u003c/var\u003e\n- `context.targetResponse.headers.`\u003cvar translate=\"no\"\u003eheader-name\u003c/var\u003e\n\nHere's a sample [AssignMessage policy](/apigee/docs/api-platform/reference/policies/assign-message-policy) showing how to read the value of a request header and store\nit into a variable: \n\n```verilog\n\u003cAssignMessage continueOnError=\"false\" enabled=\"true\" name=\"assign-message-default\"\u003e\n \u003cAssignVariable\u003e\n \u003cName\u003ereqUserAgent\u003c/Name\u003e\n \u003cRef\u003erequest.header.User-Agent\u003c/Ref\u003e\n \u003c/AssignVariable\u003e\n\u003c/AssignMessage\u003e\n```\n\nAntipattern\n\nAccessing the values of HTTP headers in Apigee policies in a way that returns only the first value\nis incorrect and can cause issues if the specific HTTP header has more than one value.\n\nThe following sections contain examples of header access.\n\nExample 1: Read a multi-valued Accept header using JavaScript code\n\nConsider that the `Accept` header has multiple values as shown below: \n\n```actionscript-3\nAccept: text/html, application/xhtml+xml, application/xml\n```\n\nHere's the JavaScript code that reads the value from `Accept` header: \n\n```gdscript\n// Read the values from Accept header\nvar acceptHeaderValues = context.getVariable(\"request.header.Accept\");\n```\n\nThe above JavaScript code returns only the first value from the `Accept` header,\nsuch as `text/html`.\n\nExample 2: Read a multi-valued Access-Control-Allow-Headers header in AssignMessage or RaiseFault policy\n\nConsider that the `Access-Control-Allow-Headers` header has multiple values as shown below: \n\n```carbon\nAccess-Control-Allow-Headers: content-type, authorization\n```\n\nHere's the part of code from AssignMessage or RaiseFault policy setting the `Access-Control-Allow-Headers` header: \n\n```text\n\u003cSet\u003e\n \u003cHeaders\u003e\n \u003cHeader name=\"Access-Control-Allow-Headers\"\u003e{request.header.Access-Control-Request-Headers}\u003c/Header\u003e\n \u003c/Headers\u003e\n\u003c/Set\u003e\n```\n\nThe above code sets the Header `Access-Control-Allow-Headers` with only the first value from the\nrequest header `Access-Control-Allow-Headers`, in this example `content-type`.\n\nImpact\n\n1. In both the examples above, notice that only the first value from multi-valued headers are returned. If these values are subsequently used by another policy in the API Proxy flow or by the backend service to perform some function or logic, then it could lead to an unexpected outcome or result.\n2. When request header values are accessed and passed onto the target server, API requests could be processed by the backend incorrectly and hence they may give incorrect results.\n3. If the client application is dependent on specific header values from the Apigee response, then it may also process incorrectly and give incorrect results.\n\nBest Practice\n\n1. Reference the `request.header.`**header_name**`.values.string`\n form of the flow variable to read all the values of a specific header.\n\n **Example: Sample fragment that could be used in RaiseFault or AssignMessage to read a multi-value header** \n\n ```text\n \u003cSet\u003e\n \u003cHeaders\u003e\n \u003cHeader name=\"Inbound-Headers\"\u003e{request.header.Accept.values.string}\u003c/Header\u003e\n \u003c/Headers\u003e\n \u003c/Set\u003e\n ```\n2. If you want individual access to each of the distinct values, you can use the appropriate\n built-in flow variables: `request.header.`\u003cvar translate=\"no\"\u003eheader_name\u003c/var\u003e`.values.count`,\n `request.header.`\u003cvar translate=\"no\"\u003eheader_name\u003c/var\u003e`.`\u003cvar translate=\"no\"\u003eN\u003c/var\u003e, `response.header.`\u003cvar translate=\"no\"\u003eheader_name\u003c/var\u003e`.values.count`,\n `response.header.`\u003cvar translate=\"no\"\u003eheader_name.N\u003c/var\u003e.\n\n Then iterate to fetch all the values from a specific header in JavaScript or JavaCallout policies.\n\n **Example: Sample JavaScript code to read a multi-value header** \n\n ```gdscript\n for (var i = 1; i \u003c=context.getVariable('request.header.Accept.values.count'); i++)\n {\n print(context.getVariable('request.header.Accept.' + i));\n }\n ```\n | **Note:**The comma is the delimiter between distinct header values. This approach will not split values based on other delimiters like semicolon, etc.\n\n For example, `application/xml;q=0.9, */*;q=0.8` will appear as two values with the above code.\n The first value is `application/xml;q=0.9`, and the second will be `*/*;q=0.8` .\n\n If the header values need to be split using semicolon as a delimiter, then you can use `string.split(\";\")` within the JavaScript callout to separate the distinct values.\n3. As an alternative, you can use the `substring()` function available within a\n [message template](/apigee/docs/api-platform/reference/message-template-intro) on the flow variable `request.header.`**header_name**`.values`\n to read all the values of a specific header.\n\n **Example: Use `substring()` within a message template to read a full multi-value header** \n\n ```text\n \u003cSet\u003e\n \u003cHeaders\u003e\n \u003cHeader name=\"Inbound-Headers\"\u003e{substring(request.header.Accept.values,1,-1)}\u003c/Header\u003e\n \u003c/Headers\u003e\n \u003c/Set\u003e\n ```\n\nFurther reading\n\n- [How to handle multi-value headers in Javascript?](https://www.googlecloudcommunity.com/gc/Cloud-Product-Articles/How-to-handle-multi-value-headers-in-Javascript/ta-p/76176)\n- [IETF RFC 7230 commentary on using comma-separated lists for multi-valued HTTP header fields](https://www.rfc-editor.org/rfc/rfc7230#section-3.2.2)\n- [Request and response variables](/apigee/docs/api-platform/fundamentals/understanding-handling-request-response-data)\n- [Flow variables reference](/apigee/docs/api-platform/reference/variables-reference)"]]