我需要张贴下列要求:发布`多部分/形式data`与Flurl
POST http://target-host.com/some/endpoint HTTP/1.1
Content-Type: multipart/form-data; boundary="2e3956ac-de47-4cad-90df-05199a7c1f53"
Accept-Encoding: gzip, deflate
Connection: Keep-Alive
Content-Length: 6971
Host: target-host.com
--2e3956ac-de47-4cad-90df-05199a7c1f53
Content-Disposition: form-data; name="some-label"
value
--2e3956ac-de47-4cad-90df-05199a7c1f53
Content-Disposition: form-data; name="file"; filename="my-filename.txt"
<file contents>
--2e3956ac-de47-4cad-90df-05199a7c1f53--
我可以按如下使用Python requests
库做到这一点真的很容易:
import requests
with open("some_file", "rb") as f:
byte_string = f.read()
requests.post(
"http://target-host.com/some/endpoint",
data={"some-label": "value"},
files={"file": ("my-filename.txt", byte_string)})
有没有办法对Flurl.Http
库做同样的事情?
我的documented这样做的问题是,它会为每个键值对插入Content-Type
标题,并且它会为文件数据插入filename*=utf-8''
标题。但是,我试图发布请求的服务器不支持此操作。还要注意标题中的name
和filename
值附近的双引号。
编辑:下面是我用来做与Flurl.Http
POST请求的代码:
using System.IO;
using Flurl;
using Flurl.Http;
namespace ConsoleApplication
{
public class Program
{
public static void Main(string[] args)
{
var fs = File.OpenRead("some_file");
var response = "http://target-host.com"
.AppendPathSegment("some/endpoint")
.PostMultipartAsync(mp => mp
.AddString("some-label", "value")
.AddFile("file", fs, "my-filename.txt")
).Result;
}
}
}
这是一个非常合法的编程问题。谁可以投票结束,请解释一下? –
你是说在文件头中包含'filename *'实际上导致了调用失败? –