api_test.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. import requests
  2. import json
  3. # url = "http://127.0.0.1:7960/brandcultivation/api/v1/recommend"
  4. # payload = {
  5. # "city_uuid": "00000000000000000000000011445301",
  6. # "product_code": "440298",
  7. # "recall_cust_count": 500,
  8. # "delivery_count": 5000
  9. # }
  10. # headers = {'Content-Type': 'application/json'}
  11. # response = requests.post(url, data=json.dumps(payload), headers=headers)
  12. # print(response.json())
  13. # 2. 然后调用报告下载接口
  14. download_url = "http://127.0.0.1:7960/brandcultivation/api/v1/download_report"
  15. download_payload = {
  16. "city_uuid": "00000000000000000000000011445301",
  17. "product_code": "440298"
  18. }
  19. download_headers = {'Content-Type': 'application/json'}
  20. print("\n调用报告下载接口...")
  21. download_response = requests.get(
  22. download_url,
  23. params=download_payload, # 注意GET请求使用params而不是data
  24. headers=download_headers
  25. )
  26. print(json.dumps(download_response.json(), indent=2))
  27. if download_response.json().get("code") == 200:
  28. for file_info in download_response.json()["data"]["reportDownloadInfo"]:
  29. print(f"\n下载文件: {file_info['filename']}")
  30. file_response = requests.get(file_info["download_url"])
  31. with open(file_info["filename"], "wb") as f:
  32. f.write(file_response.content)
  33. print(f"已保存到: {file_info['filename']}")