快速API接受来自python客户端但不来自邮递员的请求



我有以下快速API实现接收etext和图像数据:

from fastapi import FastAPI, Form, File, UploadFile
from typing import List,Dict
import os
import campaign_begin
import shutil

app = FastAPI()
@app.post("/generate_chart")
async def generate_chart(
company_retailer: str = Form(...),
ad_lvl_count: int = Form(...),
ad_level_name_list: list = Form(...),
analytics_cat_type: str = Form(...),
discount_flag: bool = Form(...),
discount_adlevel: str = Form(...),
discount_checkout_type: str = Form(...),
optimization_checkout_type: str = Form(...),
optimization_adlevel: str = Form(...),
market_type_name_checkout: str = Form(...),
a_b_test_flag: bool = Form(...),
ab_adlevel: str = Form(...),
tableau_name_checkout: str = Form(...),
clip_count: int = Form(...),
w_id: str = Form(...),
ad_level_images: List[UploadFile] = File(...),
clip_images: List[UploadFile] = File(...)
):
for index, ad_image in enumerate(ad_level_images):
with open(f"pic{index + 1}.jpg", "wb") as buffer:
shutil.copyfileobj(ad_image.file, buffer)
for index, clip_image in enumerate(clip_images):
with open(f"clip{index + 1}.jpg", "wb") as buffer:
shutil.copyfileobj(clip_image.file, buffer)
# Call campaign_begin.generate_chart() function
campaign_begin.generate_chart(
company_retailer=company_retailer,
ad_lvl_count=ad_lvl_count,
ad_level_name_list=ad_level_name_list,
analytics_cat_type=analytics_cat_type,
discount_flag=discount_flag,
discount_adlevel=discount_adlevel,
discount_checkout_type=discount_checkout_type,
optimization_checkout_type=optimization_checkout_type,
optimization_adlevel=optimization_adlevel,
market_type_name_checkout=market_type_name_checkout,
a_b_test_flag=a_b_test_flag,
ab_adlevel=ab_adlevel,
tableau_name_checkout=tableau_name_checkout,
clip_count=clip_count,
w_id=w_id
)

return {"status": "success"}

当我通过下面的python客户端发送请求时,它工作正常。

import requests
url = "http://localhost:8000/generate_chart/"
data = {
"company_retailer": "Dummy_Company",
"ad_lvl_count": 2,
"ad_level_name_list": ["dummy_adlevel_1", "dummy_adlevel_2"],
"analytics_cat_type": "Trolley + All",
"discount_flag": True,
"discount_adlevel": "dummy_adlevel_1",
"discount_checkout_type": "Trolley + All",
"optimization_checkout_type": "only-trolley",
"optimization_adlevel": "dummy_adlevel_1",
"market_type_name_checkout": "only-trolley",
"a_b_test_flag": True,
"ab_adlevel": "dummy_adlevel_1",
"tableau_name_checkout": "only-trolley",
"clip_count": 2,
"w_id": '6485klo-5477'
}
files = [
("ad_level_images", ("pic1.jpg", open("pic1.jpg", "rb"), "image/jpeg")),
("ad_level_images", ("pic2.jpg", open("pic2.jpg", "rb"), "image/jpeg")),
("clip_images", ("clip1.jpg", open("clip1.jpg", "rb"), "image/jpeg")),
("clip_images", ("clip2.jpg", open("clip2.jpg", "rb"), "image/jpeg")),
]

response = requests.post(url, data=data, files=files)
print(response.json())

但是当我通过POSTMAN发送它时,它不会通过消息:INFO: 127.0.0.1:58121 - "POST/generate_chart/HTTP/1.1"307临时重定向在索引2处没有找到边界字符137INFO: 127.0.0.1:58122 - POST/generate_chart HTTP/1.1"400错误请求

任何帮助隔离问题和解决方案的想法将非常感谢

看起来您使用了错误的端口。FastAPI默认为127.0.0.1:8000,但你的邮差错误消息说你正在使用127.0.0.1:58121

编辑:我可能错了58121可能是一个错误代码,因为下一个错误说127.0.0.1:58122。不过,如果我没记错的话,检查端口可能是件好事,因为postman默认为127.0.0.1:5555

相关内容

  • 没有找到相关文章

最新更新