Web 如何从F#代码进行外部POST API调用
我想从F#调用外部POST API,F#具有如下多部分表单请求体,那么如何在F#中执行呢 谢谢你阅读这个问题 我尝试了下面的代码,但对我无效Web 如何从F#代码进行外部POST API调用,web,f#,f#-data,f#-fake,Web,F#,F# Data,F# Fake,我想从F#调用外部POST API,F#具有如下多部分表单请求体,那么如何在F#中执行呢 谢谢你阅读这个问题 我尝试了下面的代码,但对我无效 Http.Request ( "http://endpoint/for/multipart/data", body = Multipart( boundary = "define a custom boundary here", // this is used to separate the items you're streaming
Http.Request
( "http://endpoint/for/multipart/data",
body = Multipart(
boundary = "define a custom boundary here", // this is used to separate the items you're streaming
parts = [
MultipartItem("formFieldName", "file",IFormFile.OpenReadStream())
]
))
为此,我将使用和HTTp客户端,在fsharp中,您可以使用各种东西,如或和HTTp客户端包,我建议: 只需创建一个控制台应用程序并添加这个nuget包
dotnet new console -lang f# -o http-rest-client
cd http-rest-client
dotnet add package Http.fs
dotnet add package Hopac
然后将Program.fs中的代码替换为:
//了解更多关于F#at的信息
然后,您可以运行您的项目:
dotnet run
{
"args": {},
"data": "",
"files": {
"file": "calimero calimero calimero calimero\n"
},
"form": {},
"headers": {
"Accept": "application/json",
"Content-Length": "241",
"Content-Type": "multipart/form-data; boundary=\"BPj'o/kJ+CaKDQUuOnIaoLq/diChFH\"",
"Host": "httpbin.org"
},
"json": null,
"origin": "83.53.248.254, 83.53.248.254",
"url": "https://httpbin.org/post"
}
Http.fs包中有许多发送表单的示例,该库设法添加
多部分表单数据所需的heraders
是源代码。如果有人对某个问题感到好奇,那么描述该问题通常是有帮助的。否则,试图回答问题的人必须在黑暗中猜测你的问题是什么。“对我不起作用”是不够的信息。错误消息是什么?我想执行POST调用以将图像保存到blob,但不想等待结果,所以如何从F#code执行POST调用。我在这里尝试了fsharp.github.io/fsharp.Data/library/Http.html,但没有成功。大多数方法都是等待完成post调用的执行以及它的结果,但我不关心post调用的响应
open System.IO
open System.Text
open Hopac
open HttpFs.Client
let multipartRequest =
Request.createUrl Post "http://httpbin.org/post"
|> Request.setHeader (Accept "application/json")
|> Request.body (BodyForm [
FormFile ("file", ("testfile.txt",
ContentType.create("text", "plain"),
Binary (File.ReadAllBytes ("./testfile.txt"))
))])
|> Request.responseAsString
|> run
[<EntryPoint>]
let main argv =
printfn "%s" multipartRequest
0
➜ cat testfile.txt
calimero calimero calimero calimero
dotnet run
{
"args": {},
"data": "",
"files": {
"file": "calimero calimero calimero calimero\n"
},
"form": {},
"headers": {
"Accept": "application/json",
"Content-Length": "241",
"Content-Type": "multipart/form-data; boundary=\"BPj'o/kJ+CaKDQUuOnIaoLq/diChFH\"",
"Host": "httpbin.org"
},
"json": null,
"origin": "83.53.248.254, 83.53.248.254",
"url": "https://httpbin.org/post"
}