ウェブアクションの活用
    • PDF

    ウェブアクションの活用

    • PDF

    Article Summary

    Classic/VPC環境で利用できます。

    ウェブアクションについての理解と活用に役立つ様々なフォーマット別の例、HTTPリダイレクトの例、ヘッダクッキー設定の例、画像レスポンスの例を紹介します。 サンプルコードは以下のとおりです。

    • JSON形式の例

      • タイプをJSONで呼び出す場合
        function main(params) {
               return {"key" : "value"}
         }
        
      • タイプをHTTPで呼び出す場合
        function main(params) {
                return {
                    statusCode: 200,
                    headers: { 'Content-Type': 'application/json' },
                    body: {"key":"value"}
                };
        }
        
    • HTML形式の例

      • タイプをHTMLで呼び出す場合
        function main(params) {
              return {"html":"<html><body><h3>hello</h3></body></html>"}
        }
        
      • タイプをHTTPで呼び出す場合
        function main() {
              return {
                headers: {
                  'Content-Type': 'text/html'
                },
                statusCode: 200,
                body: '<html><body><h3>hello</h3></body></html>' }
        }
        
    • TXT形式の例

      • タイプをTXTで呼び出す場合
        function main(params) {
              return {"text":"hello"}   
        }
        
      • タイプをHTTPで呼び出す場合
        function main() {
              return {
                headers: {
                  'Content-Type': 'text/plain'
                },
                statusCode: 200,
                body: 'hello' }
        }
        
    • HTTPリダイレクトの例

      function main() {
        return {
          headers: { location: 'https://www.ncloud.com' },
          statusCode: 302
        }
      }
      
    • ヘッダクッキー設定の例

      function main() {
        return {
          headers: {
            'Set-Cookie': 'UserID=Jane; Max-Age=3600; Version=',
            'Content-Type': 'text/html'
          },
          statusCode: 200,
          body: '<html><body><h3>hello</h3></body></html>' }
      }
      
      function main() {
        return {
          headers: {
            'Set-Cookie': [
              'UserID=Jane; Max-Age=3600; Version=',
              'SessionID=asdfgh123456; Path = /'
            ],
            'Content-Type': 'text/html'
          },
          statusCode: 200,
          body: '<html><body><h3>hello</h3></body></html>' }
      }
      
    • 画像レスポンスの例:コンテンツタイプをimage/pngに設定し、base64文字列で画像を返す

      function main() {
          let png = <base 64 encoded string>
          return { headers: { 'Content-Type': 'image/png' },
                   statusCode: 200,
                   body: png };
      }
      
      注意

      アクションが返すことのできる最大レスポンスサイズがあるため、最大サイズを超えるレスポンスを変えそうとするとアクションの実行結果は失敗として処理されます。 したがって、上記の例の画像のようなオブジェクトファイルを返す際は、最大レスポンスサイズに注意し、最大サイズを超える場合はアクションコードで返さず他の保存場所を利用してください。 最大レスポンスサイズのスペックは、Cloud Functionsを使用する前にをご参照ください。


    この記事は役に立ちましたか?

    Changing your password will log you out immediately. Use the new password to log back in.
    First name must have atleast 2 characters. Numbers and special characters are not allowed.
    Last name must have atleast 1 characters. Numbers and special characters are not allowed.
    Enter a valid email
    Enter a valid password
    Your profile has been successfully updated.