如何使用Python请求将多部分/数据发送到PHP

时间:2019-11-30 15:32:54

标签: php python request python-requests slim

我一直在尝试将图像发送到另一台服务器很长时间,但是由于某种原因,它不想通过请求接受文件。 在响应中,我得到{“ error”:“未提供全景图。Files = []”,“ files”:[]}。 如何正确建立请求? 正确的代码JS:https://gitlab.com/nickw1/opentrailview/blob/master/js/uploader.js

正确的CURL:     curl -i -X POST -H "Content-Type: multipart/form-data" -F "file=@/home/www-data/panos/1.jpg" -H "Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJhdWQiOiJhYmM2ODE0ZjRmODU2ZDYzMGU2OWFlY2UzYjZhMjFlZSIsImp0aSI6Ijk0ZWU4NmE1ZjlhOTQyM2JkM2ExMDVmOGFlODE2MDBjMDlhNGVhMjI1ODEyNTcwMWEzMDY5OWU2MDdhMGIyMjJiZGNlNzgzMmJjNjhmNTIxIiwiaWF0IjoxNTc1MjAwODQzLCJuYmYiOjE1NzUyMDA4NDMsImV4cCI6MTU3NTIwNDQ0Mywic3ViIjoiMTMiLCJzY29wZXMiOlsidXBsb2FkIiwiY2hhbmdlIiwiZGVsZXRlIl19.zMUtt7konYxYIDIq5ROwJAiswg-Fm5S-PP5uxypOS77rrsWJztyDwk6wQ4HCD_uTSkMso8haX6Ik_d4N6iKMm6y1hGSGPpGsIy9ayaOGhtOYr-damZMiYf-fAD2xJ6YNPgKGSNHnqg94Re4hhzydPP___d7JfG-ktZRp0wK-hIbSgGYmQDrvqAlOa-IOz7sDnsb6fsHS87Uw16LNUs6A5imreSNoeU3R_g9pmQLttRryvK9nN-M0ANXjikuIAwpu4L9rQfDeq38w_KuwbZmOsqV0XWlRy71pPIbRGWcAi3qpdoM9-cKK2Nhf7a_dwAQtmpJSq107cbb0LniSyskv6w"

Python POST code:
upload_url = 'https://opentrailview.org/oauth/api/panorama/upload'  
headers = {
    'Authorization': 'Bearer ' + token
}

with open('photos/multi-photo/sor.jpg', 'rb') as img_file:
    img = img_file.read()

files = {'file': ('sor.jpg', img, 'image/jpeg', {'Expires': '0'})}

r = requests.post(upload_url, files=files, headers=headers)
print(r.text)

另一方面,我有此代码

PHP Code:
 private function uploadPano(Request $req, Response $res, array $args) {
    $files = $req->getUploadedFiles();
    $error = $warning = null;
    $errorCode = 400;
    if(!$this->authorisedToUpload()) {
        $errorCode = 401;
        $error = "You must be authenticated to upload panos.";
    } elseif(empty($files['file'])){
        $error = "No panorama provided. files=".json_encode($files);
    } else {
        $pano= $files['file'];
        if($pano->getError() != UPLOAD_ERR_OK) {
            $error = "No file uploaded. Your file probably exceeds the max file size of ". MAX_FILE_SIZE. "MB. Error code=". $pano->getError();
        } else {
            $size = $pano->getSize();    
            if($size > MAX_FILE_SIZE * 1048576) {
                $error = "Exceeded file size of ".MAX_FILE_SIZE." MB";
            } else {
                $tmpName=$pano->file;
                $imageData = getimagesize($tmpName);
                if($imageData===false || $imageData[2]!=IMAGETYPE_JPEG) {
                    $error = "Not a JPEG image!";
                }

0 个答案:

没有答案