【问题标题】:Add images via Shopware 6 API / VBA by URL通过 Shopware 6 API / VBA 通过 URL 添加图像
【发布时间】:2021-05-15 14:25:11
【问题描述】:

我尝试使用 Shopware 6 API 使用带有 WinHttp.WinHttpRequest.5.1 的 VBA 将东西从现有的 Access 数据库迁移到新商店。除媒体传输外,一切正常。

我的做法:

  1. 在右侧媒体文件夹的商店软件中创建媒体对象
   POST /api/v3/media 

   payload: "{""mediaFolderId"":""" & folderID & """,""id"":""ec90cae2cc84c37b6b7a0cb9fe5c4548""}"

工作正常!

  1. 通过url上传新图片(testfile.png)
   POST /api/v3/_action/media/ec90cae2cc84c37b6b7a0cb9fe5c4548/upload?extension=png&fileName=testfile

   payload: "{""url"":"" & MediaUrl & "." & MediaUrlExt & ""}"

   response 204 (no error), 

创建了一个媒体文件,但文件的内容不是PNG图像而是负载字符串

VBA 代码:

Set objHTTP = CreateObject("WinHttp.WinHttpRequest.5.1")
        AuthURL = url & "/api/v2/_action/media/" & newMediaID & "/upload?extension=" & FileExtFromUrl(MediaUrl) & "&fileName=" & FileNameFromUrl(MediaUrl) & "&_response=true"
        objHTTP.Open "POST", AuthURL, False
        objHTTP.setRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"
        objHTTP.setRequestHeader "Content-type", "application/json"
        objHTTP.setRequestHeader "Accept", "application/json"
        objHTTP.setRequestHeader "Authorization", "Bearer " & Token
        Debug.Print AuthURL
        payload = "{""url"":""https://" & FTPUrl & ftpKategorieOrdner & "/" & MediaUrl & """}"
        Debug.Print payload
        objHTTP.send (payload)
        strResult = objHTTP.responseText
        Debug.Print strResult
        Debug.Print "uploadMedia " & objHTTP.Status; " - " + objHTTP.StatusText

也许创建缩略图是个问题?开发日志:

[2021-05-18 15:13:22] messenger.INFO: Sending message Shopware\Core\Content\Media\Message\GenerateThumbnailsMessage with Enqueue\MessengerAdapter\QueueInteropTransport {"message":"[object] (Shopware\\Core\\Content\\Media\\Message\\GenerateThumbnailsMessage: {})","class":"Shopware\\Core\\Content\\Media\\Message\\GenerateThumbnailsMessage","sender":"Enqueue\\MessengerAdapter\\QueueInteropTransport"} []
[2021-05-18 15:13:23] php.INFO: User Deprecated: The "Symfony\Component\Debug\DebugClassLoader" class is deprecated since Symfony 4.4, use "Symfony\Component\ErrorHandler\DebugClassLoader" instead. {"exception":"[object] (ErrorException(code: 0): User Deprecated: The \"Symfony\\Component\\Debug\\DebugClassLoader\" class is deprecated since Symfony 4.4, use \"Symfony\\Component\\ErrorHandler\\DebugClassLoader\" instead. at ./vendor/symfony/debug/DebugClassLoader.php:16)"} []

怎么了?

【问题讨论】:

  • 这不是重复的 - uw ms 尝试通过 URL 上传图像(因此 Shopware 下载图像并将其插入到库中) - 链接的问题会发布图像数据。

标签: vba api ms-access media shopware


【解决方案1】:

这应该可以工作 - 管理面板使用相同的端点通过 URL 上传图像和上传二进制数据。

我能发现的唯一区别是请求的内容类型。

很遗憾,您没有提及您设置的内容类型,我认为这是问题所在。

Only if the content type is application/json,文件是从 URL 中获取的。

确保在第二个请求中设置标头

Content-Type: application/json

编辑根据https://forum.shopware.com/t/bilder-upload-ueber-api-url/87850/4中的x-post发现VBA发送

content-type: "application/json; Charset=UTF-8"

所以 Shopware 核心的可能补丁是

124: if (strpos($contentType,'application/json') !== false))

【讨论】:

  • 设置了内容类型。我在帖子中添加了完整的代码。
  • @uwms Stange,那么它应该可以工作。您是否能够使用 Xdebug 并在函数中设置断点?如果没有,请转到var_dump($contentType); die() 路线,看看发生了什么。
  • 不知道它是否区分大小写。尝试“Content-Type”而不是“Content-type”——但我认为这不应该是原因
  • @uwms 你找到解决方案了吗?
猜你喜欢
  • 2020-12-16
  • 1970-01-01
  • 2014-01-24
  • 1970-01-01
  • 2020-12-04
  • 1970-01-01
  • 1970-01-01
  • 2020-10-14
  • 1970-01-01
相关资源
最近更新 更多