0

I'm trying to create simple Product by Rest Api status code is ok

Although I can not see the product in Magento2, Here is my code :

Token = this.GetAdminToken("admin", "password");
 var request = CreateRequest("/V1/products", Method.POST, Token);
 var product = new M2Product();
 product.Sku = "BA3245";
 product.Name = "PRoductx";
 product.Price = 100;
 product.Status = 1;
 product.Visibility = 4;
 product.AttributeSetId = 4;
 product.TypeId = "simple";

string json = JsonConvert.SerializeObject(product);
request.AddParameter("application/json", json, ParameterType.RequestBody);
 var response = Client.Execute(request);
if (response.StatusCode == System.Net.HttpStatusCode.OK){
    }else{
    }

I wonder if the installation or configuration of something is missing Thanks in Advance!!

  • use method PUT and the use URL www.domain.com/rest/V1/products/{SKU} – Ranganathan Nov 2 '19 at 9:47
  • I want to create new product it give me Unauthorized when I used "rest/V1/products" – Alex Kibar Nov 2 '19 at 11:16
  • Did you used Header as Authorization: Bearer {{admin token}}? – Ranganathan Nov 2 '19 at 11:17
  • private RestRequest CreateRequest(string endPoint, Method method,string token) { var request = new RestRequest(endPoint, method); request.RequestFormat = DataFormat.Json; request.AddHeader("Authorization", "Bearer " + token); request.AddHeader("Accept", "application/json"); return request; } } – Alex Kibar Nov 2 '19 at 11:34
  • I used RestRequest method – Alex Kibar Nov 2 '19 at 11:34
1

Get Admin token using your admin username and password

curl -X POST -H 'Content-Type: application/json' -i 'http://www.domain.com/rest/V1/integration/admin/token' --data '{
"username":"USERNAME",
"password":"PASSWORD"
}'

You can get your token and use token in below product creation request

curl -X PUT -H 'Content-Type: application/json' -H 'Authorization: Bearer ADMIN_TOKEN' -i 'http://www.domain.com/rest/V1/products/121' --data '{
 "product": {
   "sku": "121",
   "name": "B2021",
   "price": 30.00,
   "status": 1,
   "type_id": "simple",
   "attribute_set_id":4,
   "weight": 1
 }
}'

Hope you can convert this curl code into C#

  • when endpoint: /rest/V1/products response result : Unauthorized .endpoint :/api/rest/V1/products response result : OK but product is not exist magento endpoint:/hhtp://localhost/magento/rest/V1/products result : forbidden – Alex Kibar Nov 2 '19 at 13:56
  • What is the method you used? – Ranganathan Nov 4 '19 at 6:53
  • I'm not understand you .. I use Post method .. am I missing any configuration or installation – Alex Kibar Nov 4 '19 at 7:24
  • Use PUT method for adding product. Magento allows PUT method only to create a product by default – Ranganathan Nov 4 '19 at 11:15
  • when I use PUT method it give notfound message – Alex Kibar Nov 4 '19 at 11:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.