检查传入请求是否包含某个 header 值的最简单方法是什么
What is the simplest way to check that an incoming request contains a certain header value
我有一个简单的 Suave.io 服务器,格式如下:
let Ok o = o |> JsonConvert.SerializeObject |> Successful.OK
let NotOk o = o |> JsonConvert.SerializeObject |> RequestErrors.BAD_REQUEST
type Result<'T> =
| Success of 'T
| Failure of string
let DoThing someParam anotherParam =
let stats = Success(999) // <- business code here
match stats with
| Success s -> s |> Ok
| Failure m -> m |> NotOk
...
let app =
choose
[ GET >=> choose
[
pathScan "/someroute/%i/%i" (fun (p1, p2) ->
DoThing p1 p2)
]
]
startWebServer config app
0
我想检查请求是否包含具有特定名称和值的 header 以及 return NotOk(当它不存在或不正确时)。实现此目标的最简单方法是什么?
我是Suave.io作曲风格的新手。
我认为这可以满足您的需求:
#r "Newtonsoft.Json.dll"
#r "Suave.dll"
#r "Suave.Testing.dll"
#r "System.Net.Http.dll"
#r "Fuchu.dll"
open System.Net
open System.Net.Http
open Suave
open Suave.Operators
open Newtonsoft.Json
open Suave.Filters
open Suave.Testing
let Ok o = o |> JsonConvert.SerializeObject |> Successful.OK
let NotOk o = o |> JsonConvert.SerializeObject |> RequestErrors.BAD_REQUEST
type Result<'T> =
| Success of 'T
| Failure of string
let DoThing someParam anotherParam =
let stats = Success(999) // <- business code here
match stats with
| Success s -> s |> Ok
| Failure m -> m |> NotOk
let checkHeader ctx =
asyncOption {
match "key" |> ctx.request.header with
| Choice1Of2 k1 ->
printfn "Header k1: %s" k1
return ctx
| Choice2Of2 k2 ->
printfn "Header k2: %s" k2
return { ctx with
response = { ctx.response with
status = HTTP_400 } } }
let app =
choose
[ GET >=> choose
[
pathScan "/someroute/%i/%i" (fun (p1, p2) ->
DoThing p1 p2)
>=> checkHeader
]
]
let emptyCont = new ByteArrayContent([||]) |> Some
runWith defaultConfig app
|> reqResp HttpMethod.GET
"/someroute/1/2" ""
None
None
DecompressionMethods.None
id
id
确保您在与此脚本相同的目录中具有所需的库,并且您可以 运行 它。因为没有key
header,结果会是一个BAD_REQUEST
,内容是999.
checkHeader 可以解决问题。
我有一个简单的 Suave.io 服务器,格式如下:
let Ok o = o |> JsonConvert.SerializeObject |> Successful.OK
let NotOk o = o |> JsonConvert.SerializeObject |> RequestErrors.BAD_REQUEST
type Result<'T> =
| Success of 'T
| Failure of string
let DoThing someParam anotherParam =
let stats = Success(999) // <- business code here
match stats with
| Success s -> s |> Ok
| Failure m -> m |> NotOk
...
let app =
choose
[ GET >=> choose
[
pathScan "/someroute/%i/%i" (fun (p1, p2) ->
DoThing p1 p2)
]
]
startWebServer config app
0
我想检查请求是否包含具有特定名称和值的 header 以及 return NotOk(当它不存在或不正确时)。实现此目标的最简单方法是什么?
我是Suave.io作曲风格的新手。
我认为这可以满足您的需求:
#r "Newtonsoft.Json.dll"
#r "Suave.dll"
#r "Suave.Testing.dll"
#r "System.Net.Http.dll"
#r "Fuchu.dll"
open System.Net
open System.Net.Http
open Suave
open Suave.Operators
open Newtonsoft.Json
open Suave.Filters
open Suave.Testing
let Ok o = o |> JsonConvert.SerializeObject |> Successful.OK
let NotOk o = o |> JsonConvert.SerializeObject |> RequestErrors.BAD_REQUEST
type Result<'T> =
| Success of 'T
| Failure of string
let DoThing someParam anotherParam =
let stats = Success(999) // <- business code here
match stats with
| Success s -> s |> Ok
| Failure m -> m |> NotOk
let checkHeader ctx =
asyncOption {
match "key" |> ctx.request.header with
| Choice1Of2 k1 ->
printfn "Header k1: %s" k1
return ctx
| Choice2Of2 k2 ->
printfn "Header k2: %s" k2
return { ctx with
response = { ctx.response with
status = HTTP_400 } } }
let app =
choose
[ GET >=> choose
[
pathScan "/someroute/%i/%i" (fun (p1, p2) ->
DoThing p1 p2)
>=> checkHeader
]
]
let emptyCont = new ByteArrayContent([||]) |> Some
runWith defaultConfig app
|> reqResp HttpMethod.GET
"/someroute/1/2" ""
None
None
DecompressionMethods.None
id
id
确保您在与此脚本相同的目录中具有所需的库,并且您可以 运行 它。因为没有key
header,结果会是一个BAD_REQUEST
,内容是999.
checkHeader 可以解决问题。