次のリクエストを投稿する必要があります。
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--
requests次のようにPythonライブラリを使用すると、これを非常に簡単に実行できます。
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ライブラリで同じことを行う方法はありますか?
文書化された方法に関する私の問題は、Content-Typeキーと値のペアごとにヘッダーが挿入さfilename*=utf-8''れ、ファイルデータのヘッダーが挿入されることです。ただし、リクエストを送信しようとしているサーバーはこれをサポートしていません。また、ヘッダーの値nameと値を二重引用符で囲んでいることに注意してください。filename
編集:以下は、投稿リクエストを行うために使用したコードですFlurl.Http:
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;
}
}
}