GovTalk / HMRC Transaction Engine Http 1.1 POST error

474 views Asked by At

I appreciate this is a little bit niche, but I thought I would ask anyway. I'm writing a small c# application to utilise the HMRC web portal and electronically submit VAT returns in XML format. According to the HMRC specification it is just a simple Http 1.1 POST action required, and retrieving the response in XML. The application is built, however, I am having trouble with this code. I get an "OK" HttpWebResponse, but the HMRC server returns this spurious error message which they can't seem to tell me what it means. Here is my code:

                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(destinationUrl);
            byte[] bytes;
            bytes = System.Text.Encoding.ASCII.GetBytes(requestXml);
            request.ContentType = "text/xml; charset='utf-8'";

            request.ContentLength = bytes.Length;
            request.Method = "POST";
            Stream requestStream = request.GetRequestStream();
            requestStream.Write(bytes, 0, bytes.Length);
            requestStream.Close();
            HttpWebResponse response;
            response = (HttpWebResponse)request.GetResponse();

            if (response.StatusCode == HttpStatusCode.OK)
            {
                Stream responseStream = response.GetResponseStream();
                string responseStr = new StreamReader(responseStream).ReadToEnd();
                return responseStr;
            }

And the error is:

1001 - The submitted XML document either failed to validate against the GovTalk schema for this class of document or its body was badly formed.

I know the XML is ok, because when I test it using a third-party tool like "Postman" it submits 100% and the Transaction Engine returns no errors, so it must be my code. Does anything look glaringly wrong to post an XML? I have tried different Content/MIME Types and also I have confirmed that 'utf-8' is the correct encoding.

I was just wondering if any developers out there had worked on the Transaction Engine and could share their submit/post code?

1

There are 1 answers

0
Davy C On

The answer is that the code converting the file contents into the Byte Array was actually converting the file name.

I changed this line:

bytes = System.Text.Encoding.ASCII.GetBytes(requestXml);

To this:

bytes = File.ReadAllBytes(requestXml);