Here is something that should work. I haven't tested the code, and basically just changed the API URL and JSON, since my code works off the LS Retail API and not the LS Ecom API.
Hope this helps!
var url = @"https://ApiKey:[email protected]/en/products/ProductId.json";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "PUT";
request.Credentials = new NetworkCredential(shopifyApiKey, shopifyApiPwd);
request.PreAuthenticate = true;
request.ContentType = "application/json";
var json = @{
\product\": {
\"visibility\": \"hidden\",
\"data01\": \"\",
\"data02\": \"\",
\"data03\": \"\",
\"title\": \"Wade Crewneck Navyblue\",
\"fulltitle\": \"Wade Crewneck Navyblue\",
\"description\": \"De Wade Crewneck Navyblue van Wemoto. Deze blauwe trui met de enorme W op de chest heeft een heel klassieke look.\",
\"content\": \"De Wade Crewneck Navyblue van Wemoto. Deze blauwe trui met de enorme W op de chest heeft een heel klassieke look.\",
\"deliverydate\": 1,
\"supplier\": 2,
\"brand\": 3
}
}";
using (var writer = new StreamWriter(request.GetRequestStream()))
{
writer.Write(json);
writer.Flush();
writer.Close();
}
var response = new HttpResponseMessage();
using (var responseApi = (HttpWebResponse)request.GetResponse())
{
using (var reader = new StreamReader(responseApi.GetResponseStream()))
{
var objText = reader.ReadToEnd();
var objJson = (Newtonsoft.Json.Linq.JObject)Newtonsoft.Json.JsonConvert.DeserializeObject(objText);
response.Content = new StringContent(objText);
}
}
// Do whatever you want with the HttpWebResponse...
The code formatting is kinda screwed up posting this, but you get the idea
var request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "PUT";
request.Credentials = new NetworkCredential(ApiKey, ApiSecret);
request.PreAuthenticate = true;
request.ContentType = "application/json";
var json = new JavaScriptSerializer().Serialize(lsProduct);
using (var writer = new StreamWriter(request.GetRequestStream()))
{
writer.Write(json);
writer.Flush();
writer.Close();
}
var responseMessage = new HttpResponseMessage();
using (var webResponse = (HttpWebResponse)request.GetResponse())
{
var stream = webResponse.GetResponseStream();
if (stream == null) return;
using (var reader = new StreamReader(stream))
{
var objText = reader.ReadToEnd();
var objJson = (JObject)JsonConvert.DeserializeObject(objText);
responseMessage.Content = new StringContent(objText);
}
}
}
Before digging into this, if it is possible I would like to use the RestSharp.RestClient object, because I have got that working for reading data from LightSpeed and I can re-use the object.
For reading I have this, which works like a charm:
public async Task UpdateLsProduct(LsProduct lsProduct)
{
var request = new RestRequest
{
Resource = $"products/{lsProduct.Id}.json",
Method = Method.PUT
};
request.AddJsonBody(lsProduct);
var response = await _restClient.ExecuteGetTaskAsync(request);
}
results: response.StatusCode: OK response.ResponseStatus: Completed BUT response.Content contains (in json) the unchanged object and the product in the shop remains unchanged. What am I doing wrong?
7 comments
Hope this helps!
Lightspeed HQ
Lightspeed HQ
400 - invalid command.
I have now this:
Before digging into this, if it is possible I would like to use the RestSharp.RestClient object, because I have got that working for reading data from LightSpeed and I can re-use the object.
For reading I have this, which works like a charm:
I tried to create an update method like this:
results:
response.StatusCode: OK
response.ResponseStatus: Completed
BUT
response.Content contains (in json) the unchanged object and the product in the shop remains unchanged.
What am I doing wrong?
I tried the above. It ends up with an error 400: invalid command.
This happens while executing the line that says:
This is what I have now: