在使用 Swagger Type Provider 时,我应该怎么做才能防止出现 401 Unauthorized?
What should I do to prevent a 401 Unauthorised when using the Swagger Type Provider?
尝试通过 Swagger(使用 Swagger 类型提供程序)执行 HTTP post 时,我收到 401 Unauthorized。
我怀疑这个问题的答案可能是 UseDefaultCredentials,它在从 SwaggerProvider.Internal.ProvidedSwaggerBaseType 继承时不会公开。所以我在想向上转型 :> 可能会有所帮助,但我在其他地方看到的 posts 表明天真。
此外,更新 fsi.exe 的配置以包含以下内容证明是如意的:
<system.net>
<defaultProxy enabled="true" useDefaultCredentials="true">
<proxy usesystemdefault="True" />
</defaultProxy>
</system.net>
有人可以简单回答吗?
谢谢
架构访问
SwaggerProvider
假定 Swagger 模式是 easy-accessible 并且可以使用简单的 Http 请求下载而无需身份验证
type PetStore = SwaggerProvider<"http://petstore.swagger.io/v2/swagger.json">
用户也可以像这样将任何 HTTP header 添加到 schema-request
type PetStore = SwaggerProvider<"http://petstore.swagger.io/v2/swagger.json", "Content-Type=application/json">
可以是Authorization: Basic QWxhZGRpbjpPcGVuU2VzYW1l
header,但是这个文字串在源代码中会是hard-coded。
如果 schema-request 需要更复杂的身份验证,下载它并将其放在源代码附近会更容易
[<Literal>]
let schemaPath = __SOURCE_DIRECTORY__ + "/PetStore.Swagger.json"
type PetStore = SwaggerProvider<schemaPath>
请求控制
当模式到位时,您可以使用提供的类型中的 CustomizeHttpRequest
参数完全控制对服务器的所有 HTTP 请求。
例如,如果您想使用默认凭据:
let store =
PetStore(
CustomizeHttpRequest=
fun (req:System.Net.HttpWebRequest) ->
req.UseDefaultCredentials <- true
req)
您可以根据需要自由修改网络请求:
- 使用默认凭据
- 指定运行时凭据
- 向 HTTP 请求添加 headers
- 向 HTTP 请求添加 cookie
- 中断请求 ;) 等等
CustomizeHttpRequest
将在 run-time 中为每个请求调用,在 SwaggerProvider
构建它之后和调用服务器之前。所以你可以改变任何你想要的。
尝试通过 Swagger(使用 Swagger 类型提供程序)执行 HTTP post 时,我收到 401 Unauthorized。
我怀疑这个问题的答案可能是 UseDefaultCredentials,它在从 SwaggerProvider.Internal.ProvidedSwaggerBaseType 继承时不会公开。所以我在想向上转型 :> 可能会有所帮助,但我在其他地方看到的 posts 表明天真。
此外,更新 fsi.exe 的配置以包含以下内容证明是如意的:
<system.net>
<defaultProxy enabled="true" useDefaultCredentials="true">
<proxy usesystemdefault="True" />
</defaultProxy>
</system.net>
有人可以简单回答吗?
谢谢
架构访问
SwaggerProvider
假定 Swagger 模式是 easy-accessible 并且可以使用简单的 Http 请求下载而无需身份验证
type PetStore = SwaggerProvider<"http://petstore.swagger.io/v2/swagger.json">
用户也可以像这样将任何 HTTP header 添加到 schema-request
type PetStore = SwaggerProvider<"http://petstore.swagger.io/v2/swagger.json", "Content-Type=application/json">
可以是Authorization: Basic QWxhZGRpbjpPcGVuU2VzYW1l
header,但是这个文字串在源代码中会是hard-coded。
如果 schema-request 需要更复杂的身份验证,下载它并将其放在源代码附近会更容易
[<Literal>]
let schemaPath = __SOURCE_DIRECTORY__ + "/PetStore.Swagger.json"
type PetStore = SwaggerProvider<schemaPath>
请求控制
当模式到位时,您可以使用提供的类型中的 CustomizeHttpRequest
参数完全控制对服务器的所有 HTTP 请求。
例如,如果您想使用默认凭据:
let store =
PetStore(
CustomizeHttpRequest=
fun (req:System.Net.HttpWebRequest) ->
req.UseDefaultCredentials <- true
req)
您可以根据需要自由修改网络请求:
- 使用默认凭据
- 指定运行时凭据
- 向 HTTP 请求添加 headers
- 向 HTTP 请求添加 cookie
- 中断请求 ;) 等等
CustomizeHttpRequest
将在 run-time 中为每个请求调用,在 SwaggerProvider
构建它之后和调用服务器之前。所以你可以改变任何你想要的。