0
0

More than 1 year has passed since last update.

DigitalOcean FunctionのAPIでJSON形式でレスポンスさせる方法

Last updated at Posted at 2023-08-24

JSON形式になぜならないのか

main.js
export function main(args) {
  const response = {
        "body":'abc',
    };
    return response;
}

result
abc

となってしまいJSON形式にならない。

解決方法

の一番下に書いてある通り、

https://docs.digitalocean.com/products/functions/reference/parameters-responses/#returns
If you only ever want to return JSON data from your function, 
you can ignore the above requirements 
and instead return any result that can be serialized to JSON. 
To access this option, add .json to the end of your function URL when invoking it.

FunctionのURL(Web Function・REST APIのどちらでも)の後ろに「.json」とつける

https://faas-*********.doserverless.co/api/v1/web/***************/default/********.json

のようにすると

result
{
    "body": "abc"
}

こうしてJSON形式でレスポンスできたとさ。めでたいわ。

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