我需要張貼下列要求:發佈`多部分/形式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 *'實際上導致了調用失敗? –