-1

I am quite new to programming and I am meant to make a post request to Taxdoo APi. I am using the latest version of RestSharp. I tried reading their documentation but the lack of example code has me, as a beginner, crying in a corner. Here's the problem:

The API expects an array of products with the following payload format:

{
  "products": [
    {
      "description": "Couch Supreme red",
      "productIdentifier": "redcouch123",
      "commodityCode": "9394240000",
      "purchasePrice": 3200,
      "currency": "EUR"
    },
    {
      "description": "Wood table",
      "productIdentifier": "wt234bas",
      "commodityCode": "9394220000",
      "purchasePrice": 300,
      "currency": "EUR"
    }
  ]
}

How can I get the products : into the request? I tried the request.AddParameter, the request.AddOrUpdateParameter, I tried letting RestSharp do the parsing with request.AddJsonBody but I am so lost. I get BadRequest Error every time.

My code so far:

namespace ConnectToApi
{
    internal class Program
    {
        static void Main(string[] args)
        {
            var endpoint = new Uri("https://sandbox-api.taxdoo.com/products");
            using (RestClient? restClient = new(endpoint))
            {
                var request = new RestRequest();
                var authToken = ConfigurationManager.AppSettings["authToken"];
                request.AddHeader("AuthToken", authToken);
                      
                MyProduct productOne = new MyProduct
                {
                    Description = "Product 1",
                    ProductIdentifier = "GBP-120",
                    CommodityCode = "123456",
                    PurchasePrice = 40,
                    Currency = "USD"
                };
                MyProduct productTwo = new MyProduct
                {
                    Description = "Product 2",
                    ProductIdentifier = "GBP-125",
                    CommodityCode = "789101112",
                    PurchasePrice = 20,
                    Currency = "USD"
                };
                
                List<MyProduct> productList = new();
                productList.Add(productOne);
                productList.Add(productTwo);
                
                string json = JsonConvert.SerializeObject(productList,Formatting.Indented);
                request.AddStringBody(json, DataFormat.Json);
                var response = restClient.Execute(request);
            }
        }
    }

The JSONString looks like so:

[
  {
    "description": "Product 1",
    "productIdentifier": "GBP-120",
    "commodityCode": "123456",
    "purchasePrice": 40,
    "currency": "USD"
  },
  {
    "description": "Product 2",
    "productIdentifier": "GBP-125",
    "commodityCode": "789101112",
    "purchasePrice": 20,
    "currency": "USD"
  }
]

If anyone could help or send me a ressource where to look that is beginner friendly, I would be really glad. Cheers guys.

  • There are bazillion tools over the internet which translate JSON to C# classes ... – Selvin Dec 07 '22 at 11:27
  • https://stackoverflow.com/questions/6312970/restsharp-json-parameter-posting – Roman Ryzhiy Dec 07 '22 at 11:27
  • I am not trying to translate JSON to C#, I am lost on how to add the products: parameter to my Post Request. I already serialized the object to JSON. – HappyFrogie Dec 07 '22 at 11:30
  • You wrote "The API expects an array of products with the following payload format" - wich is not entirely correct. The examples shows an object, containing a field "products", wich is an array of "product" objects. Roman's answer below looks promising :) – nilsK Dec 07 '22 at 12:26

2 Answers2

0

You don't need serialization, just a wrapping class:

public class Body
{
    public List<MyProduct> Products { get; set; }
}
...
var body = new Body();
body.Products = productList;
request.AddStringBody(body, DataFormat.Json);
Roman Ryzhiy
  • 1,540
  • 8
  • 5
  • I tried both your versions, For Roman's version, it tells me that body is not a string, for Serge's version it just gets me the reply: FORBIDDEN. – HappyFrogie Dec 07 '22 at 13:45
0

You need to add a product wraping around your list before serialization. And add Post to your request. You can use an anonymous class for example

var request = new RestRequest(endpoint, Method.POST);

....

var products = new { Products = productList};
 string json = JsonConvert.SerializeObject(products);
 request.AddStringBody(json, DataFormat.Json);
 var response = restClient.Execute(request);
Serge
  • 40,935
  • 4
  • 18
  • 45