我正在嘗試將 pdf 檔案發布到 api。
這是我的代碼的樣子:
procedure AttachDocument(pathFile : String);
var
RESTClient : TRESTClient;
RESTRequest : TCustomRESTRequest;
Response : TCustomRESTResponse;
begin
RESTClient := TRESTClient.Create('');
try
RESTRequest := TCustomRESTRequest.Create(nil);
try
RESTClient.BaseURL('api_url');
RESTRequest.Client := RESTClient;
RESTRequest.Accept := 'application/json';
RESTRequest.Params.AddHeader('j_token','mytoken').Options := 1; //This is adding poDoNotEncode
RESTRequest.Method := rmPOST
RESTRequest.AddParameter('file',pathFile,pkFILE,1); // Same as above 1 = poDoNotEncode
//RESTRequest.AddFile(pathFile, ctAPPLICATION_PDF);
RESTRequest.Execute;
Response := RESTRequest.Response;
ShowMessage(Response.Content);
finally
RESTRequest.Free;
end;
finally
RESTClient.Free;
end;
end;
出于某種原因,我只能使用procedure AddFile(const AFileName: string; AContentType: TRESTContentType = TRESTContentType.ctNone);and 因為似乎我需要將檔案作為“檔案”發布到 api,所以我決定改用該TCustomRESTRequest.AddParameter方法(雖然,為了我的專案,我也用注釋方法)。
目前,當我嘗試使用此代碼訪問 api 時,我收到:
{“錯誤”:“未找到檔案”}
然后我問 api 所有者我應該為 api 發布什么樣的資料來接受我的檔案,他用他用來將檔案發布到他的 api 的 C# 代碼做出了回應:
var client = new RestClient("api_url");
client.Timeout = -1;
var request = new RestRequest(Method.POST);
request.AddHeader("Content-Type", "multipart/form-data");
request.AddHeader("v", "4.2");
request.AddHeader("j_token", "histoken");
request.AddFile("file", "/C:/Users/olivm/Documents/QA API Swagger/smartpacte2.pdf", "application/.pdf");
IRestResponse response = client.Execute(request);
Console.WriteLine(response.Content);
到目前為止,我沒有看到我們的代碼之間有任何重大差異,所以我決定在我的本地 python api 上測驗我的代碼,如下所示:
from flask import Flask, jsonify, abort, request
app = Flask(__name__)
@app.route('/', methods=['GET', 'POST'])
def home():
print("POST / ENDPOINT")
headers = request.headers
file = request.files
print(headers)
print(file)
return "Request headers:\n" str(headers) str(file)
if __name__ == "__main__":
app.run(port=5000, host="0.0.0.0")
這就是我在測驗代碼時收到的RESTClient.BaseURL('127.0.0.1:5000');
請求標頭:
連接:保持活動
內容型別:multipart/form-data;邊界=-------Embt-Boundary--15DE48857EEC829D
Accept: application/json
Accept-Charset: utf-8, *;q=0.8
User-Agent: Embarcadero RESTClient/1.0
J-Token: mytoken
Content-Length: 836323
主機:127.0.0.1: 5000
--
ImmutableMultiDict([('file', <FileStorage: 'contract.pdf' ('application/pdf')>)])
我不是很熟悉,ImmutableMultiDict但是當我通過發送相同的檔案使用 Postman 測驗 api 時,我得到了相同的結果,但在請求標頭中進行了一些細微的更改,所以我假設我正在將檔案發布到我的 api。
我的最后一個測驗是將檔案發布到 webhook。使用 Postman,它作業得很好。使用我的代碼,似乎沒有發布任何檔案,如您在此處看到的:


最后,我完全迷失了:假設我的 python api 收到了一個檔案,我是否正確?如果是這樣,那么為什么 webhook 不接收它呢?
uj5u.com熱心網友回復:
request.files 是一個字典,其中包含客戶端發送的多部分請求的不同“部分”。
它可能包含多個檔案,因此您必須掃描字典
我已經修改了您的示例,以便保存檔案以便您檢查它:
@app.route('/', methods=['GET', 'POST'])
def home():
print("POST / ENDPOINT")
headers = request.headers
for key, file in request.files.items():
if file.filename != '':
dest = os.path.join(os.path.dirname(__file__), file.filename)
file.save(dest)
print(f'saved into {dest}.')
return "Request headers:\n" str(headers) str(file)
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/519529.html
