Here is the code we are using, its pretty simple, it gets the image data via image url and passes the binary data to the InsertPicture API call:
Public Shared Function loadImageNopAPI(ByVal imgUrl As String) As HttpWebResponse
'get authorization token
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12
Dim authorizationToken = getNopAuthToken()
'get image data based on image url
Dim imageByteArray As Byte() = GetDobaImageDataBinary(imgUrl)
'get mimetype
Dim mimeType = MimeMapping.GetMimeMapping(imgUrl)
'do the put call
Dim response As HttpWebResponse
If authorizationToken <> "0" Then
'create request object
Dim apiCallURL = "(our store base url is here)/api-backend/Picture/InsertPicture?" & "mimeType=" & HttpContext.Current.Server.UrlEncode(mimeType) & "&seoFilename=productimage&isNew=true&validateBinary=true"
Dim httpWebRequest As HttpWebRequest
httpWebRequest = WebRequest.Create(apiCallURL)
httpWebRequest.ContentType = mimeType
httpWebRequest.Method = "PUT"
httpWebRequest.Accept = "application/json"
httpWebRequest.Headers("Authorization") = authorizationToken
'set the content of the request
Dim imageStream As Stream = httpWebRequest.GetRequestStream()
imageStream.Write(imageByteArray, 0, imageByteArray.Length)
imageStream.Close()
'Get response
Dim pictureID As Integer
Try
response = httpWebRequest.GetResponse()
Dim responseStream = response.GetResponseStream()
Dim reader As New StreamReader(responseStream)
Dim responseFromServer = reader.ReadToEnd()
Dim pictureJson As JObject
pictureJson = JObject.Parse(responseFromServer)
pictureID = pictureJson.SelectToken("id")
'Return pictureID
Return response
Catch ex As WebException
Return ex.Response
End Try
End If
Return response
End Function
Public Shared Function GetDobaImageDataBinary(ByVal imgUrl As String) As Byte()
Dim request As HttpWebRequest = WebRequest.Create(imgUrl)
Dim response As HttpWebResponse = request.GetResponse()
Dim imageStream As Stream = response.GetResponseStream()
Dim memoryStream = New MemoryStream()
imageStream.CopyTo(memoryStream)
Return memoryStream.ToArray
End Function
I have tried multiple tests with different types of images as the incoming data and no matter what we try we get the 415 error, any help would be appreciated, thanks!