0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Azure Static Web AppsのマネージドAPIのRequestとContextに何が入っているのか確認したメモ

Posted at

概要

Azure Static Web Apps の マネージドAPIのAzure FunctionsのHttpTriggerで渡される引数に何が入っているのか知りたかった。
まとまっているドキュメントを見つけられなかったので実際に出力して確認。
どこかにドキュメントがあるなら、だれか教えてくださるとうれしいです。

検証コード

import {
  app,
  HttpRequest,
  HttpResponseInit,
  InvocationContext,
} from '@azure/functions';

export async function httpTrigger1(
  request: HttpRequest,
  context: InvocationContext,
): Promise<HttpResponseInit> {
  context.log(`Http function processed request for url "${request.url}"`);

  const name = request.query.get('name') || (await request.text()) || 'world';
  return {
    body: JSON.stringify({
      msg: `Hello, ${name}!`,
      headers: JSON.stringify([...request.headers.entries()]),
      url: JSON.stringify(request.url),
      method: request.method,
      query: JSON.stringify([...request.query.entries()]),
      context: JSON.stringify(context),
    }),
  };
}

app.http('httpTrigger1', {
  methods: ['GET', 'POST'],
  authLevel: 'anonymous',
  handler: httpTrigger1,
});

REST

get https://gentle-smoke-0024c9c00.5.azurestaticapps.net/api/httpTrigger1?test=a

request

パラメータ コメント
method リクエストのメソッド名 GET
params パスパラメータ {id}の場合params.id
query クエリパラメータのMap query.get('test')でaが取得できる
url FunctionsのURL。変動することがある。StaticWebAppsのURLとは異なる。 https://494376bb-f9c9-4a2b-917c-817b195c529c.azurewebsites.net/api/httpTrigger1?test=a\
headers リクエストヘッダのMap 下記表を参照
Header Value
authorization Bearer eyJhbGciOiJI...
client-ip 10.0.0.12:62830
connection keep-alive
disguised-host 494376bb-f9c9-4a2b-917c-817b195c529c.azurewebsites.net
host 494376bb-f9c9-4a2b-917c-817b195c529c.azurewebsites.net
max-forwards 8
traceparent 00-de77855821e0ccdf9edbb6afc6ecabc4-9099303eccaba7da-00
user-agent vscode-restclient
was-default-hostname 494376bb-f9c9-4a2b-917c-817b195c529c.azurewebsites.net
x-appservice-proto https
x-arr-log-id 9bd19712-beee-4f3a-a7ae-2335939a607b
x-arr-ssl 2048
x-forwarded-for 133.226.1.27:63381, 20.205.82.199:46830
x-forwarded-proto https
x-forwarded-tlsversion 1.3
x-ms-auth-token Bearer eyJhbGciOiJSUzI1...
x-ms-client-principal-id anonymous
x-ms-client-principal-idp azureStaticWebApps
x-ms-original-url https://gentle-smoke-0024c9c00.5.azurestaticapps.net/api/httpTrigger1?test=a
x-ms-request-id 9bd19712-beee-4f3a-a7ae-2335939a607b
x-original-url /api/httpTrigger1?test=a
x-site-deployment-id 494376bb-f9c9-4a2b-917c-817b195c529c
x-waws-unencoded-url /api/httpTrigger1?test=a

headers

[
    [
        "authorization",
        "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYmYiOjE3MTQzNTQ5ODYsImV4cCI6MTcxNDM1NTI4NiwiaWF0IjoxNzE0MzU0OTg2LCJpc3MiOiJodHRwczovLzQ5NDM3NmJiLWY5YzktNGEyYi05MTdjLTgxN2IxOTVjNTI5Yy5zY20uYXp1cmV3ZWJzaXRlcy5uZXQiLCJhdWQiOiJodHRwczovLzQ5NDM3NmJiLWY5YzktNGEyYi05MTdjLTgxN2IxOTVjNTI5Yy5henVyZXdlYnNpdGVzLm5ldC9henVyZWZ1bmN0aW9ucyJ9.huHEvlvMU07CAf-weHjAR0CboD4JqrO6q_wTJBtL3Ks"
    ],
    [
        "client-ip",
        "10.0.0.12:62830"
    ],
    [
        "connection",
        "keep-alive"
    ],
    [
        "disguised-host",
        "494376bb-f9c9-4a2b-917c-817b195c529c.azurewebsites.net"
    ],
    [
        "host",
        "494376bb-f9c9-4a2b-917c-817b195c529c.azurewebsites.net"
    ],
    [
        "max-forwards",
        "8"
    ],
    [
        "traceparent",
        "00-de77855821e0ccdf9edbb6afc6ecabc4-9099303eccaba7da-00"
    ],
    [
        "user-agent",
        "vscode-restclient"
    ],
    [
        "was-default-hostname",
        "494376bb-f9c9-4a2b-917c-817b195c529c.azurewebsites.net"
    ],
    [
        "x-appservice-proto",
        "https"
    ],
    [
        "x-arr-log-id",
        "9bd19712-beee-4f3a-a7ae-2335939a607b"
    ],
    [
        "x-arr-ssl",
        "2048|256|CN=Microsoft Azure RSA TLS Issuing CA 07, O=Microsoft Corporation, C=US|CN=*.azurewebsites.net, O=Microsoft Corporation, L=Redmond, S=WA, C=US"
    ],
    [
        "x-forwarded-for",
        "133.226.1.27:63381, 20.205.82.199:46830"
    ],
    [
        "x-forwarded-proto",
        "https"
    ],
    [
        "x-forwarded-tlsversion",
        "1.3"
    ],
    [
        "x-ms-auth-token",
        "Bearer eyJhbGciOiJSUzI1NiIsImtpZCI6IkZERDAxRDZEODczRjY1MTIzQzg5QzJEQzFGNkJGNjBCMzJFMjFBNDEiLCJ0eXAiOiJKV1QifQ.eyJwcm4iOiJiblZzYkE9PSIsInN1YiI6ImFub255bW91cyIsImlzcyI6Imh0dHBzOi8vZ2VudGxlLXNtb2tlLTAwMjRjOWMwMC41LmF6dXJlc3RhdGljYXBwcy5uZXQvLmF1dGgiLCJhdWQiOiJodHRwczovLzQ5NDM3NmJiLWY5YzktNGEyYi05MTdjLTgxN2IxOTVjNTI5Yy5henVyZXdlYnNpdGVzLm5ldCIsIm5iZiI6MTcxNDM1NDg2OSwiZXhwIjoxNzE0MzgwMzY5LCJpYXQiOjE3MTQzNTQ4Njl9.FuaFvKiZ0hXrAlxqshuRQelSD3XAQIydTjA4hyyPdR2I4WTnJUID9gRscdHGENhsHml-eplGOmUFVQgVGicuKlXLKS5Xw2QYuo8l0-Anz-B5eSwoULv26kr6ezq5TzRDJB4fNahJbDlE_LBIZzFixiR0-0WsowccFMS-zfATWxZVs-l6MUaLIcSuf_nasfR9SmZp1albicZ6cp7vwfEAuS70AL-5ljnHDkIVCpKjyoORVGAu62F40GYfWxvVhshanSp41DsuPz_9ATlwrW8G4deeNw9Z6nKG_bqKNnSMZbhVugGS4T85fofU41pv-ixWkiC3nRNP3b479yE07Z1P9w"
    ],
    [
        "x-ms-client-principal-id",
        "anonymous"
    ],
    [
        "x-ms-client-principal-idp",
        "azureStaticWebApps"
    ],
    [
        "x-ms-original-url",
        "https://gentle-smoke-0024c9c00.5.azurestaticapps.net/api/httpTrigger1?test=a"
    ],
    [
        "x-ms-request-id",
        "9bd19712-beee-4f3a-a7ae-2335939a607b"
    ],
    [
        "x-original-url",
        "/api/httpTrigger1?test=a"
    ],
    [
        "x-site-deployment-id",
        "494376bb-f9c9-4a2b-917c-817b195c529c"
    ],
    [
        "x-waws-unencoded-url",
        "/api/httpTrigger1?test=a"
    ]
]

headers authorization

jwtをデコードすると下記になる
リクエストヘッダのauthorizationヘッダは上書きされてしまうので、独自の認証情報を送りたい場合はcustom-authorizationなどのようにヘッダ名をずらす工夫が必要になる。
stack over flow

{
  "nbf": 1714354986,
  "exp": 1714355286,
  "iat": 1714354986,
  "iss": "https://494376bb-f9c9-4a2b-917c-817b195c529c.scm.azurewebsites.net",
  "aud": "https://494376bb-f9c9-4a2b-917c-817b195c529c.azurewebsites.net/azurefunctions"
}

headers x-ms-auth-token

jwtをデコードすると下記になる。

{
  "prn": "bnVsbA==",
  "sub": "anonymous",
  "iss": "https://gentle-smoke-0024c9c00.5.azurestaticapps.net/.auth",
  "aud": "https://494376bb-f9c9-4a2b-917c-817b195c529c.azurewebsites.net",
  "nbf": 1714354869,
  "exp": 1714380369,
  "iat": 1714354869
}

headers x-ms-original-url

Static Web Apps としての固定されたURLを参照したい場合、このパラメータを使うことになる。

context

あまり有益な情報は取れなさそう。関数名とりたいときくらい?
context.logの利用がメインになりそう。

JSONPath Value
$.invocationId e877dd89-7f8e-45f5-b217-555a5154edb8
$.functionName httpTrigger1
$.extraInputs {}
$.extraOutputs {}
$.traceContext.traceParent ""
$.traceContext.traceState ""
$.traceContext.attributes {}
$.options.trigger.methods[0] GET
$.options.trigger.methods[1] POST
$.options.trigger.authLevel anonymous
$.options.trigger.type httpTrigger
$.options.trigger.name httpTrigger624a61988c
$.options.trigger.direction in
$.options.return.type http
$.options.return.name $return
$.options.return.direction out
$.options.extraInputs []
$.options.extraOutputs []
{
    "invocationId": "e877dd89-7f8e-45f5-b217-555a5154edb8",
    "functionName": "httpTrigger1",
    "extraInputs": {},
    "extraOutputs": {},
    "traceContext": {
        "traceParent": "",
        "traceState": "",
        "attributes": {}
    },
    "options": {
        "trigger": {
            "methods": [
                "GET",
                "POST"
            ],
            "authLevel": "anonymous",
            "type": "httpTrigger",
            "name": "httpTrigger624a61988c",
            "direction": "in"
        },
        "return": {
            "type": "http",
            "name": "$return",
            "direction": "out"
        },
        "extraInputs": [],
        "extraOutputs": []
    }
}

参考

AzureStaticWebAppsの公式ドキュメント

Azure Static Web Apps での API サポートの概要

Functionsの公式ドキュメント

Azure Functions の HTTP トリガー

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?