Fedex tracking API using .NET Core API – throwing 500 Error

I am writing logic to make Fedex API call in my .NET API Core application.
I got C#.NET sample code from Fedex Developer documentation.
I have all required data like Key, Password, AccountNumber, MeterNumber.

In the sample C#.NET console application(provided by Fedex on their developer guide site), they have added WSDL reference and TrackRequest, TrackService and TrackReply are available and Also SecurityProtocol TLS Settings are taken care.
we are able to call TrackService.Track method and successful test it.

Now my objective is to write this in .NET Core API project.

In Core API, i tried to add Service Reference to the WSDL (TrackServiceDefinitions.wsdl) file that is downloaded from Fedex documentation. That threw some nSwag related error, which i am not sure.

I am not sure how to build SOAP Request. This is how Fedex Tracking SOAPrequest looks like:
How to get full tracking info (Fedex Webservices)?

So i wrote the API Method as below.
I am getting 500 Internal Server Error.

What am i doing wrong here? Is there any other better/easy way to make this API Call?

Appreciate your responses.

        [HttpGet]
        [Route("GetFedexTrackStatus/{trackingNumber}")]
        public IActionResult GetFedexTrackStatus(int trackingNumber)
        {
            
            var sXml = @"<TrackRequest xmlns='http://fedex.com/ws/track/v16'><WebAuthenticationDetail><UserCredential><Key>{Key}</Key>
        <Password>{Password}</Password></UserCredential></WebAuthenticationDetail><ClientDetail>
        <AccountNumber>{AccountNumber}</AccountNumber><MeterNumber>{MeterNumber}</MeterNumber></ClientDetail>
        <TransactionDetail><CustomerTransactionId>Track By Number_v16</CustomerTransactionId></TransactionDetail>
        <Version><ServiceId>trck</ServiceId><Major>16</Major><Intermediate>0</Intermediate><Minor>0</Minor></Version>
        <PackageIdentifier><Value>192221280879</Value><Type>TRACKING_NUMBER_OR_DOORTAG</Type></PackageIdentifier>
</TrackRequest>";

            string Err = "";
            WebResponse WebRes = null;
            string sResponse = "";
            try
            {
                string URL = "https://wsbeta.fedex.com:443/web-services/track"; 
                byte[] buffer = Encoding.UTF8.GetBytes(sXml);
                WebRequest WebReq = WebRequest.Create(URL);
                WebReq.Method = "POST";
                WebReq.Timeout = 1000000;
                WebReq.ContentType = "application/x-www-form-urlencoded";
                WebReq.ContentLength = buffer.Length;
                Stream ReqStream = WebReq.GetRequestStream();
                ReqStream.Write(buffer, 0, buffer.Length);
                ReqStream.Close();
                WebRes = WebReq.GetResponse();
                Stream ResStream = WebRes.GetResponseStream();
                StreamReader ResReader = new StreamReader(ResStream);
                sResponse = ResReader.ReadToEnd();
            }
            catch (Exception ex)
            {
                Err = ex.Message.ToString();
            }

            return Ok(sResponse);
        }

Thanks
Rita

edited 1 hour ago
Rita

asked 2 hours ago
RitaRita

333 bronze badges

Latest posts