Hi, I was trying to update StockQuantity using API, the product shall be selected by the SKU.
Below is my code, but it failed to update the StockQuantity as required:
protected void Page_Load(object sender, EventArgs e) { string publicKey = "XYZ"; string secretKey = "XYZ"; string content = "{\"StockQuantity\":\"5\"}"; // assume that new stock is 5 string accept = "application/json, text/javascript, */*"; byte[] data = Encoding.UTF8.GetBytes(content); string contentMd5Hash = CreateContentMd5Hash(data); string method = "PUT"; string timestamp = DateTime.UtcNow.ToString("o"); // 2013-11-11T19:44:04.9378268Z string url = "http://xyz.xyz.com:1260/odata/v1/Products$filter=Sku eq '1234'"; // assume that SKU is 1234 var uri = new Uri(url); // decode url if (uri.Query != null && uri.Query.Length > 0) { url = string.Concat(uri.GetLeftPart(UriPartial.Path), HttpUtility.UrlDecode(uri.Query)); } var messageRepresentation = string.Join("\n", method.ToLower(), "", accept.ToLower(), url.ToLower(), timestamp, publicKey.ToLower() ); string signature = CreateSignature(secretKey, messageRepresentation); var request = (HttpWebRequest)WebRequest.Create(url); request.Method = method; 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); request.Headers.Add("Authorization", "SmNetHmac1 " + signature); request.ContentLength = data.Length; request.Headers.Add("Content-MD5", contentMd5Hash); using (var stream = request.GetRequestStream()) { stream.Write(data, 0, data.Length); } } public string CreateContentMd5Hash(byte[] content) { string result = ""; if (content != null && content.Length > 0) { using (var md5 = MD5.Create()) { byte[] hash = md5.ComputeHash(content); result = Convert.ToBase64String(hash); } } return result; } public string CreateSignature(string secretKey, string messageRepresentation) { if (string.IsNullOrWhiteSpace(secretKey) || string.IsNullOrWhiteSpace(messageRepresentation)) return ""; string signature; var secretBytes = Encoding.UTF8.GetBytes(secretKey); var valueBytes = Encoding.UTF8.GetBytes(messageRepresentation); using (var hmac = new HMACSHA256(secretBytes)) { var hash = hmac.ComputeHash(valueBytes); signature = Convert.ToBase64String(hash); } return signature; }