0

I am new to APIs . I have a rest API which have a request body and response body in XML format. I want to hit the API but I have no clue how to send the request body from the code. The request body of my API is -

<Person>
<Id>12345</Id>
<Customer>John Smith</Customer>
<Quantity>1</Quantity>
<Price>10.00</Price>
</Person>

My Effort :

I know so far that to deal with APIs you have to create a proxy class. So my proxy class is -

 [System.SerializableAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
    [System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
    public partial class Person
    {

        private ushort idField;

        private string customerField;

        private byte quantityField;

        private decimal priceField;

        /// <remarks/>
        public ushort Id
        {
            get
            {
                return this.idField;
            }
            set
            {
                this.idField = value;
            }
        }

        /// <remarks/>
        public string Customer
        {
            get
            {
                return this.customerField;
            }
            set
            {
                this.customerField = value;
            }
        }

        /// <remarks/>
        public byte Quantity
        {
            get
            {
                return this.quantityField;
            }
            set
            {
                this.quantityField = value;
            }
        }

        /// <remarks/>
        public decimal Price
        {
            get
            {
                return this.priceField;
            }
            set
            {
                this.priceField = value;
            }
        }
    }

and from this answer

How can I Post data using HttpWebRequest?

I am doing the following -

 var request = (HttpWebRequest)WebRequest.Create("https://reqbin.com/sample/post/xml");

            Person person = new Person();
            
            Console.WriteLine("Enter ID");
            person.Id = Convert.ToUInt16(Console.ReadLine());

            Console.WriteLine("Enter Name");
            person.Customer = Console.ReadLine();

            Console.WriteLine("Enter Quantity");
            person.Quantity = Convert.ToByte(Console.ReadLine());

            Console.WriteLine("Enter Price");
            person.Price = Convert.ToDecimal(Console.ReadLine());

          
            var data = Encoding.ASCII.GetBytes(person);

I am getting error in var data = Encoding.ASCII.GetBytes(person)

It says cannot convert form Person to Char[]

I am not sure now how to proceed.

Anonymous
  • 59
  • 7

1 Answers1

0

GetBytes expects string-like input to turn it into a byte array. So you'll have to turn peron into a string/char-array first. Since you want to work with XML, you should use an XML serializer. E.g. using the serializer built into .NET:

// Setup for the person above

// Serialize the person into an XML string
var serializer = new XmlSerializer(typeof(Person));
var sb = new StringBuilder();
using (XmlWriter xmlWriter = XmlWriter.Create(sb))
{
    serializer.Serialize(xmlWriter, person);
}

// Byte array data to send
var data = Encoding.ASCII.GetBytes(sb.ToString());
Xerillio
  • 4,855
  • 1
  • 17
  • 28