簡體   English   中英

Python 請求 - 無法在有效負載中發送圖像

[英]Python Requests - Unable to send image in payload

我使用 Python 庫請求創建了一個 API 測試。 我在嘗試創建新產品時遇到問題。 產品有效負載包含一個圖像。 請參閱 Postman POST 請求:-

在此處輸入圖像描述

我正在嘗試將圖像作為 JSON 有效負載的一部分發送。 請參閱下面的代碼:-

import requests
import json
import base64
from Utilities.resources import *
from Utilities.payloads import *
from Utilities.configurations import *

config = getConfig()

login_user_url = config['API']['productionUrl'] + ApiResources.userLogin
create_product_url = config['API']['productionUrl'] + ApiResources.createProduct

print(create_product_url)

### Create a new product ###

## First get a user token - Login and get token
login_response = requests.post(login_user_url, json=userPayload(), headers=headerPayload())
get_login_json_response = login_response.json()
token = get_login_json_response['token']

## Use token to create product
print("CREATE A PRODUCT")

## https://stackoverflow.com/questions/29104107/upload-image-using-post-form-data-in-python-requests
image_file = "D:\\My Training Courses\\Python APUI Testing - Requests\\images\\testImage.jpg"

with open(image_file, "rb") as f:
    im_bytes = f.read()
im_b64 = base64.b64encode(im_bytes).decode("utf8")

headerWithBearerTokenWithFormDataPayload = json.dumps(
        {
                'Content-Type': 'multipart/form-data; boundary=<calculated when request is sent>',
                'Authorization': 'Bearer ' + token
        }
)

bodyPayload = json.dumps(
        {
                "name": "Product API Test",
                "description": "Product API Test description",
                "richDescription": "Product API Test rich description",
                "image": im_b64,
                "brand": "Product API Test brand",
                "price": 300,
                "category": "5f15d54cf3a046427a1c26e3",
                "countInStock": 10,
                "rating": 4,
                "numReviews": 22,
                "isFeatured": True
        }
)

## create_product_response = requests.post(create_product_url, json=newProductPayload(im_b64), headers=headerWithBearerTokenPayload(token))
create_product_response = requests.post(create_product_url, json=bodyPayload, headers=headerWithBearerTokenWithFormDataPayload)
print(create_product_response)

我收到以下錯誤:- 在此處輸入圖像描述

根據請求的文檔,您需要將字典作為json參數而不是字符串傳遞。 headers參數相同。 所以刪除json.dumps

bodyPayload = json.dumps(
        {
                "name": "Product API Test",
                "description": "Product API Test description",
                "richDescription": "Product API Test rich description",
                "image": im_b64,
                "brand": "Product API Test brand",
                "price": 300,
                "category": "5f15d54cf3a046427a1c26e3",
                "countInStock": 10,
                "rating": 4,
                "numReviews": 22,
                "isFeatured": True
        }
)

暫無
暫無

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

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