Web API (backend) - InsertPicture failing : 415 - Unsupported Media Type

9 meses atrás
We keep getting '415 - Unsupported Media Type' when calling the InsertPicture web api call, let me know if anyone has successfully done this call and can help...

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!
9 meses atrás
Hi. It looks like you didn't regenerate the client after updating the WEB API. But the signature of the some method you are using has changed a bit. Unfortunately, I am not familiar with VBA and cannot tell where the error is in your client. However, everything points to the fact that you do not set or set the wrong type of the transferred content, it should be multipart/form-data. The right code looks like bellow


var boundary_ = System.Guid.NewGuid().ToString();
var content_ = new System.Net.Http.MultipartFormDataContent(boundary_);
content_.Headers.Remove("Content-Type");
content_.Headers.TryAddWithoutValidation("Content-Type", "multipart/form-data; boundary=" + boundary_);

var content_fileBinary_ = new System.Net.Http.StreamContent(fileBinary.Data);
if (!string.IsNullOrEmpty(fileBinary.ContentType))
  content_fileBinary_.Headers.ContentType = System.Net.Http.Headers.MediaTypeHeaderValue.Parse(fileBinary.ContentType);
content_.Add(content_fileBinary_, "fileBinary", fileBinary.FileName ?? "fileBinary");

request_.Content = content_;
9 meses atrás
Sergei-k wrote:
Hi. It looks like you didn't regenerate the client after updating the WEB API. But the signature of the some method you are using has changed a bit. Unfortunately, I am not familiar with VBA and cannot tell where the error is in your client. However, everything points to the fact that you do not set or set the wrong type of the transferred content, it should be multipart/form-data. The right code looks like bellow


var boundary_ = System.Guid.NewGuid().ToString();
var content_ = new System.Net.Http.MultipartFormDataContent(boundary_);
content_.Headers.Remove("Content-Type");
content_.Headers.TryAddWithoutValidation("Content-Type", "multipart/form-data; boundary=" + boundary_);

var content_fileBinary_ = new System.Net.Http.StreamContent(fileBinary.Data);
if (!string.IsNullOrEmpty(fileBinary.ContentType))
  content_fileBinary_.Headers.ContentType = System.Net.Http.Headers.MediaTypeHeaderValue.Parse(fileBinary.ContentType);
content_.Add(content_fileBinary_, "fileBinary", fileBinary.FileName ?? "fileBinary");

request_.Content = content_;


thanks for the reply, just to clarify I have tried it with 'multipart/form-data' as the content type and we get a 400 error then (400 - Bad Request).

Also, to clarify I did get the updated API from another NOP team member via email and was able to install it and it passed the original error and now has the 415 error. However, that was just me installing the new web API plugin version.

>>It looks like you didn't regenerate the client after updating the WEB API.
Not sure what you mean, are you sayin I still have to recompile the base store code? Not sure how that would change anything if the base store code has not changed?
9 meses atrás
Sergei-k wrote:
Hi. It looks like you didn't regenerate the client after updating the WEB API. But the signature of the some method you are using has changed a bit. Unfortunately, I am not familiar with VBA and cannot tell where the error is in your client. However, everything points to the fact that you do not set or set the wrong type of the transferred content, it should be multipart/form-data. The right code looks like bellow


var boundary_ = System.Guid.NewGuid().ToString();
var content_ = new System.Net.Http.MultipartFormDataContent(boundary_);
content_.Headers.Remove("Content-Type");
content_.Headers.TryAddWithoutValidation("Content-Type", "multipart/form-data; boundary=" + boundary_);

var content_fileBinary_ = new System.Net.Http.StreamContent(fileBinary.Data);
if (!string.IsNullOrEmpty(fileBinary.ContentType))
  content_fileBinary_.Headers.ContentType = System.Net.Http.Headers.MediaTypeHeaderValue.Parse(fileBinary.ContentType);
content_.Add(content_fileBinary_, "fileBinary", fileBinary.FileName ?? "fileBinary");

request_.Content = content_;


I think i am getting closer using your code example, what are you using for the 'request_' object?
9 meses atrás
>>I think i am getting closer using your code example, what are you using for the 'request_' object?

I figured it out using the HttpRequestMessage object and was able to get it to work, big thanks!!