Search code examples
c#xmlpostdotnet-httpclientxmldocument

Upload XmlDocument to endpoint


im trying to upload an XmlDocument to an endpoint but i must be doing something wrong because it returns "Bad Request", here is what i have tried:

the endpoing we are talking about is this one (Validar Semilla): link to the swagger
if i manually upload the file in the swagger test it works, so my xml is in good shape.

snippet 1:

var httpClient = new HttpClient();
var someXmlString = MyXMLDoc.InnerXml;
var stringContent = new StringContent(someXmlString, Encoding.UTF8, "multipart/form-data");
var respone = await httpClient.PutAsync("/someurl", stringContent);

snippet 2:

public static string postXMLData(string destinationUrl, string requestXml)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(destinationUrl);
            byte[] bytes;
            bytes = System.Text.Encoding.ASCII.GetBytes(requestXml);
            request.ContentType = "multipart/form-data; encoding='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;
            }
            return null;
        }

Sample XML File:

<?xml version="1.0" encoding="utf-8"?>
<SemillaModel xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <valor>lWVzKj0dStSuFkR3nXx2mrf2R2q9J+x6JVuPk8jmx0Bwr8DUmI1A9+eld/lot8MNNhC17k8fLeQLGKXW2naiLXXJ41rf0GNS6vlz0CGvuMaCDKElzpnR4NNBBo6XRNcoCEEaA+3H0kBItG1W+bU0pA==</valor>
  <fecha>2021-07-10T10:46:28.4448582-04:00</fecha>
  <Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
    <SignedInfo>
      <CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315" />
      <SignatureMethod Algorithm="http://www.w3.org/2001/04/xmldsig-more#rsa-sha256" />
      <Reference URI="">
        <Transforms>
          <Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature" />
        </Transforms>
        <DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256" />
        <DigestValue>VBFrBAoKPWRlOQO6RgVkY/3Jh6nNERnG2uGK+thFd24=</DigestValue>
      </Reference>
    </SignedInfo>
    <SignatureValue>ValuesIDoNotFeelSafeSharing</SignatureValue>
    <KeyInfo>
      <X509Data>
        <X509Certificate>ValuesIDoNotFeelSafeSharing</X509Certificate>
      </X509Data>
    </KeyInfo>
  </Signature>
</SemillaModel>

I'm new to XML, what am i doing wrong? any article that could help me?


Solution

  • You need to pass file content, not string content. Try something like below.

    using (var content = new MultipartFormDataContent())  
    {  
        byte[] Bytes = new byte[xmlfile.InputStream.Length + 1];  
        file.InputStream.Read(Bytes, 0, Bytes.Length);  
        var fileContent = new ByteArrayContent(Bytes);  
        fileContent.Headers.ContentDisposition =
        new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment") { FileName = file.FileName };  
    
       content.Add(fileContent); 
    
       var requestUri = "http://{api url} ";  
       var result = client.PostAsync(requestUri, content).Result;  
                    
    }