如何通过向 API 发送 POST 请求来上传 pdf

How to upload a pdf by sending a POST Request to an API

我尝试通过向 R 和 Python 中的 API 发送 POST 请求来上传 pdf,但我没有取得很大的成功。

这是我在 R 中的代码

library(httr)

url <- "https://envoc-apply-api.azurewebsites.net/api/apply"
POST(url, body = upload_file("filename.pdf"))

当我想要状态 202 时,我收到的状态是 500

我也试过使用确切的路径而不是文件名,但是出现了文件不存在的错误

我的代码在Python

import requests

url ='https://envoc-apply-api.azurewebsites.net/api/apply'
files = {'file': open('filename.pdf', 'rb')}
r = requests.post(url, files=files)

我收到错误

FileNotFoundError: [Errno 2] No such file or directory: 'filename.pdf'

我一直在尝试使用这些作为示例进行指导。

R https://cran.r-project.org/web/packages/httr/vignettes/quickstart.html

Python http://requests.readthedocs.io/en/latest/user/quickstart/

如果您需要更多信息,请告诉我。 任何帮助将不胜感激。

您需要指定文件的完整路径:

import requests

url ='https://envoc-apply-api.azurewebsites.net/api/apply'
files = {'file': open('C:\Users\me\filename.pdf', 'rb')}
r = requests.post(url, files=files)

或类似的东西:否则它在尝试打开它时永远找不到 filename.pdf。