创建一个处理JSON请求的WebPart:let jsonPart : WebPart =
choose [
path "/api" >=> choose [
GET >=> OK "Welcome to the API endpoint"
POST >=> OK "Received a POST request"
]
NOT_FOUND "Endpoint not found"
]
设置响应的Content-Type为application/json:let jsonHeaders : Headers =
Headers [
"Content-Type", "application/json"
]
let jsonPart : WebPart =
choose [
path "/api" >=> choose [
GET >=> setStatusCode 200 >=> setHeader jsonHeaders >=> OK "Welcome to the API endpoint"
POST >=> setStatusCode 200 >=> setHeader jsonHeaders >=> OK "Received a POST request"
]
NOT_FOUND "Endpoint not found"
]
返回JSON数据作为响应:let jsonData = {| "message" : "Hello, World!" |}
let jsonPart : WebPart =
choose [
path "/api" >=> choose [
GET >=> setStatusCode 200 >=> setHeader jsonHeaders >=> OK (jsonData |> JsonConvert.SerializeObject)
POST >=> setStatusCode 200 >=> setHeader jsonHeaders >=> OK "Received a POST request"
]
NOT_FOUND "Endpoint not found"
]