Quantcast
Channel: Active questions tagged rest - Stack Overflow
Viewing all articles
Browse latest Browse all 4051

Posting to XML Data to REST Endpoint from .NET Core

$
0
0

I have an external user who is trying to POST data to my endpoint via SOAP (XML format). However, my code is a .NET 6 REST APi. I transformed the XML payload to a class and managed to generate the class. Below is the XML output:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><soapenv:Header/><soapenv:Body><PaymentNotificationRequest><User>USER</User><Password>pass</Password><HashVal>NzIwNGE3MzI5YmU5MTg3ZTUwZTQ1YmRjMjA0NDc2MjUyODQ2MmQ1ODIwZTIyYzNkNDk0NTBjNjUwZTgwYmM2Yw==</HashVal><TransType>999</TransType><TransID>FTC221024YAWQ</TransID><TransTime>2210241702</TransTime><TransAmount>1.00</TransAmount><AccountNr>6592460859</AccountNr><Narrative>Test</Narrative><PhoneNr>66666</PhoneNr><CustomerName>Customer 1</CustomerName><Status>SUCCESS</Status></PaymentNotificationRequest></soapenv:Body></soapenv:Envelope>

I have my Controller as:

    [HttpPost]    [Consumes("application/xml")]    [Produces("application/xml")]    public dynamic Post([FromBody] MYDATA mymodel)    {        int createdId = mpesaRepository.Add(mymodel);        if (createdId > 0)        {            mymodel.TransType = createdId;        }        dynamic result = new        {            data = new            {                ID = createdId,                mymodel.User,                mymodel.Password,                mymodel.HashVal,                mymodel.TransType,                mymodel.TransID,                mymodel.TransTime,                mymodel.TransAmount,                mymodel.AccountNr,                mymodel.Narrative,                mymodel.PhoneNr,                mymodel.CustomerName,                mymodel.Status            },        };        return result;    }

On Posting the data via PostMan, the following 404 error is generated

<MVC-Errors><MVC-Empty>An error occurred while deserializing input data.</MVC-Empty><mymodel>The mymodel field is required.</mymodel></MVC-Errors>

On my Program.cs, I have already enabled the

            services.AddControllers(options =>        {            options.RespectBrowserAcceptHeader = true;        }).AddXmlSerializerFormatters()        .AddXmlDataContractSerializerFormatters();

Below is my Class translated from the XML:

    [XmlRoot(ElementName = "PaymentNotificationRequest")]public class PaymentNotificationRequest{    [XmlElement(ElementName = "ID")]    public int ID { get; set; }    [XmlElement(ElementName = "User")]    public string User { get; set; }    [XmlElement(ElementName = "Password")]    public string Password { get; set; }    [XmlElement(ElementName = "HashVal")]    public string HashVal { get; set; }    [XmlElement(ElementName = "TransType")]    public long TransType { get; set; }    [XmlElement(ElementName = "TransID")]    public string TransID { get; set; }    [XmlElement(ElementName = "TransTime")]    public string TransTime { get; set; }    [XmlElement(ElementName = "TransAmount")]    public string TransAmount { get; set; }    [XmlElement(ElementName = "AccountNr")]    public long AccountNr { get; set; }    [XmlElement(ElementName = "Narrative")]    public string Narrative { get; set; }    [XmlElement(ElementName = "PhoneNr")]    public string PhoneNr { get; set; }    [XmlElement(ElementName = "CustomerName")]    public string CustomerName { get; set; }    [XmlElement(ElementName = "Status")]    public string Status { get; set; }}[XmlRoot(ElementName = "Envelope", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]public class Envelope{    [XmlElement(ElementName = "PaymentNotificationRequest")]    public PaymentNotificationRequest NCBAPaymentNotificationRequest { get; set; }    [XmlAttribute(AttributeName = "soapenv", Namespace = "http://www.w3.org/2000/xmlns/")]    public string Soapenv { get; set; }}

Viewing all articles
Browse latest Browse all 4051

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>