簡體   English   中英

python 請求不將我的 POST 數據作為內容類型發送:multipart/form-data auto

[英]python requests not sending my POST data as content-type:multipart/form-data auto

我編寫了一個腳本,該腳本將搜索文件夾中的所有圖像,並從 excel 文件中獲取所有信息並將該數據發布到 API

我所做的一切,去年我得到了什么卡住了Python請求想送我的POST數據自動content-type:multipart/form-data ,但它並沒有因為我的文章數據到字典對象的腳本發送請求的Content-Type: application/x-www-form-urlencoded

import requests
import logging
import os
import json
import csv

try:
    import http.client as http_client
except ImportError:
    # Python 2
    import httplib as http_client
http_client.HTTPConnection.debuglevel = 1

# You must initialize logging, otherwise you'll not see debug output.
logging.basicConfig()
logging.getLogger().setLevel(logging.DEBUG)
requests_log = logging.getLogger("requests.packages.urllib3")
requests_log.setLevel(logging.DEBUG)
requests_log.propagate = True

url_and = "https://api.test.com/api/3.0/listings/"

android_token = '8fas8d7f9a8s7d9f87as9d8f7sa9df7s9f'
headers = {
    'Authorization': "Token " + android_token,
    'platform': 'android',
}

data_android = {}

with open("listingData1.csv", "r") as f_input:
    csv_input = csv.DictReader(f_input)

    for row in csv_input:
        data_android = {
        'mailing_details':row['shipping'],
        'abcoupay':'false',
        'price':row['price'],
        'description':row['desc'],
        'title':row['title'],
        'meetup':'false',
        'condition':'2',
        'mailing':'true',
        'collection_id':row['cat']
        }

        search_path = row['image']
        urls = []
        for file in os.listdir(os.getcwd()+search_path):
            if file.endswith((".jpg",".jpeg",".png",".JPG",".JPEG",".PNG")):
                x = os.getcwd()+"\\"+search_path+"\\"+file
                urls.append(x)


        files = {'photo_%s' % x: ('photo_%s.jpg' % x, open(file, 'rb'), 'image/jpeg') for x, file in enumerate(urls)}

        response = requests.request("POST", url_and,data=data_android,headers=headers)
        print(response.text.encode("utf-8"))

python請求發送http請求,如

POST /api/3.0/listings/ HTTP/1.1
Host: api.test.com
Connection: keep-alive
Accept-Encoding: gzip, deflate
Accept: */*
User-Agent: python-requests/2.21.0
platform: android
Authorization: Token as8fa7d87af7s9d8f79as8df0
Content-Length: 986
Content-Type: application/x-www-form-urlencoded

meetup=false&testupay=false&description=%95+Product+Details%0A%95+Suitable+for+very+small+dogs+and+cats%2C+rabbits+and+other+rodent+carriers+such+as+guinea+pigs%2C+ferrets%2C+rats+and+chinchillas.+Made+of+sturdy+plastic%2C+it+is+sturdy+and+convenient.+A+suitable+grill+allows+air+to+circulate+on+both+sides.+The+door+is+made+Of+plastic+coated+steel.+Thanks+to+the+ergonomic+handle%2C+the+mobile+utility.+It+has+a+mix+of+colors+to+choose+from.+--------%0A%95+80cm+length+X+56cm+width+X+65cm+height%0A%95+Approx.+4.1kg+after+assembly%0A%95+Suitable+for+pets+below+16kg%0A%95+Ideal+for+pet+transportation%0A%95+PP+plastic+material%3B+durable+and+sturdy%0A%95+Great+ventilation+and+air+flow%0A%95+Smart+lock+system%0A%95+Can+be+dismantled+for+easy+storage%0A%95+%2A+Please+check+that+this+item+meets+your+airline%27s+specific+requirements+prior+to+air+travel+usage+%2A%0A&mailing_details=FREE&collection_id=45&price=190&title=Pet+Cat+Dog+Carrier+Portable+Breathable&mailing=true&condition=2'

問題是您在發出 POST 請求時使用的是數據而不是文件

如果您像我在下面所做的那樣更改使用文件字段的請求,則應將 Content-Type 設置為 multipart/form-data

response = requests.request("POST", url_and, files=data_android, headers=headers)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM