Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

我用py上传文件上传不了,但是使用postman却可以正常上传文件 #106

Closed
wuyiwywy opened this issue May 6, 2019 · 10 comments

Comments

@wuyiwywy
Copy link

wuyiwywy commented May 6, 2019

image
image

@qicosmos
Copy link
Owner

qicosmos commented May 6, 2019

抓包看看二者哪里不同。

@wuyiwywy
Copy link
Author

wuyiwywy commented May 6, 2019

py requests 传过去多了一个头信息,但是我其他的接口用这两种访问方式就没有这样的问题

@qicosmos
Copy link
Owner

qicosmos commented May 6, 2019

用node js传呢

@wuyiwywy
Copy link
Author

wuyiwywy commented May 6, 2019

没用过node.js,主要最近用你推荐的file方式上传图片,写好整个项目,但是今天利用python调用接口发现py request上传的图片req.get_upload_files()获取不到
image

@qicosmos
Copy link
Owner

qicosmos commented May 6, 2019

你打包一个上传文件的测试程序给我,我看看。

@wuyiwywy
Copy link
Author

wuyiwywy commented May 6, 2019

我用postman就可以正常上传图片,问题就是我最开始截图的那两个问题

@wuyiwywy wuyiwywy closed this as completed May 6, 2019
@wuyiwywy
Copy link
Author

wuyiwywy commented May 6, 2019

void process(request& req, response& res) {
auto& files = req.get_upload_files();
std::string img_path;
for (auto& file : files) {
std::cout << file.get_file_path() << " " << file.get_file_size() << std::endl;
img_path = file.get_file_path();
}
auto data = req.body();
std::cout << "body length:" << data<< std::endl;
}

@wuyiwywy
Copy link
Author

wuyiwywy commented May 6, 2019

import base64
import requests
from urllib.parse import unquote

with open("../data/img/test1.png", "rb") as f:
img = f.read()

headers = {"Content-Type": "form-data"}
req_url = "http://127.0.0.1:8081/"
res = requests.post(req_url, headers=headers, files={"file": img})
print(res.text)

py 的测试程序

@wuyiwywy
Copy link
Author

wuyiwywy commented May 6, 2019

要不我加你qq,截图给你看也行

@qicosmos
Copy link
Owner

qicosmos commented May 6, 2019

你加cinatra qq群吧 340713904

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants