通过 Shopware 6 API / VBA 通过 URL 添加图像

问题描述

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

我的方法

  1. 在 shopware 中的正确媒体文件夹中创建媒体对象
   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)"} []

怎么了?

解决方法

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

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

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

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

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

Content-Type: application/json

EDIT 根据 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))