Best Answer Marcus Gesing , 05 February 2021 - 16:19
Best Answer Marcus Gesing , 05 February 2021 - 16:19
Geschrieben: 05 February 2021 - 15:21
Hallo / Hi there
I'm struggling with get my Picture-Import working. Maybe you can help me to understand, what
belongs to request.header and what in the body, because I always receive an common 404 back from the API.
Further I am not sure if the header AND body has to be used for calculating contentMd5Hash, messagePresentation and signature.
HttpClient client = new HttpClient(); String baseApiPath = "https://shop/odata/v1/"; String publicKey = "c21da847ce8f5a0f8cb668**********"; String secretKey = "6b46535b8074418cf90f10*********"; String accept = "application/json"; String method = "post"; String timestamp = DateTime.UtcNow.ToString("o"); String url = baseApiPath + "Uploads/ProductImages"; url = decodeUrl(url); // Function not pasted here String boundary = "----------" + DateTime.Now.Ticks.ToString("x"); var request = (HttpWebRequest)WebRequest.Create(url); request.ContentType = "Content-Type: multipart/form-data; boundary=" + boundary; request.Method = method; request.UserAgent = "SmartStoreImporter"; request.Accept = accept; request.Headers.Add("Accept-Charset", "UTF-8"); request.Headers.Add("SmartStore-Net-Api-PublicKey", publicKey); request.Headers.Add("SmartStore-Net-Api-Date", timestamp); Byte[] pic = File.ReadAllBytes("pic1.PNG"); // All before the ImageBinaryContent String content1 = "Content-Type: multipart/form-data; boundary=" + boundary + "\nContent-Length: " + pic.Length.ToString() + "\n\n" + boundary + "\nContent-Disposition: form-data; name=\"Id\"\n\n9\n" + boundary + "\nContent-Disposition: form-data; name=\"mypicture\"; filename=\"my-picture-file.jpg\";PictureId=\"999\"; CustomValue1=\"text1\"; CustomValue2=\"text2\"\nContent-Type: image/png\n\n"; Byte[] content1Bytes = Encoding.UTF8.GetBytes(content1); // All after the ImageBinaryContent (Last Boundary) String content2 = boundary + "--"; Byte[] content2Bytes = Encoding.UTF8.GetBytes(content2); Int32 contentLength = Encoding.UTF8.GetBytes(content1).Length + pic.Length + Encoding.UTF8.GetBytes(content2).Length; // Used to bring "Text" and Binarydata in one Bite-Array. Is this good? Byte[] data2 = new Byte[contentLength]; System.Buffer.BlockCopy(content1Bytes, 0, data2, 0, content1Bytes.Length); System.Buffer.BlockCopy(pic, 0, data2, content1Bytes.Length, pic.Length); System.Buffer.BlockCopy(content2Bytes, 0, data2, content1Bytes.Length + pic.Length, content2Bytes.Length); String contentMd5Hash = CreateContentMd5Hash(data2); String messageRepresentation = String.Join("\n", method.ToLower(), contentMd5Hash ?? "", accept.ToLower(), url.ToLower(), timestamp, publicKey.ToLower()); String signature = CreateSignature(secretKey, messageRepresentation); request.Headers.Add("Authorization", "SmNetHmac1 " + signature); request.ContentLength = data2.Length; request.Headers.Add("Content-MD5", contentMd5Hash); using (var stream = request.GetRequestStream()) { stream.Write(data2, 0, data2.Length); }
It is hard to debug, if there is no working example, no reference and just a nothing-saying-error.
As you can see in the screenshot, the format should be okay. No?
I also attached the well-known response-error.
Thanks for every kind of help
nullpunktnull
Geschrieben: 05 February 2021 - 16:19 Best Answer
Marcus Gesing
Smartstore AG
Geschrieben: 09 February 2021 - 14:57
HI Marcus
Thanks for your help. The problem it's not so easy to extract just the needed parts without using the whole client.
Is there not a small help/script-example with just how to upload a file with needed headers for authentication and so on?
Best regards
nullpunktnull