0

how to get

"xml":

before value in "form" ? i have to build request like "expected" which was created by Postman. "Result" was generated by c# script. In postman i selected Body --> x-www-form-urlencoded --> Key name("xml") --> Value (MyXML). how to do it in c# script?

Code:

        internal string sendOrder(string xml)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://httpbin.org/post");

            byte[] requestInFormOfBytes = Encoding.UTF8.GetBytes(xml);
            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded";
            request.ContentLength = requestInFormOfBytes.Length;

            Stream requestStream = request.GetRequestStream();
            requestStream.Write(requestInFormOfBytes, 0, requestInFormOfBytes.Length);
            //requestStream.Write


            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            StreamReader respStream = new StreamReader(response.GetResponseStream(), System.Text.Encoding.Default);
            string receivedResponse = respStream.ReadToEnd();
            Console.WriteLine(receivedResponse);
            respStream.Close();
            response.Close();
            requestStream.Close();
            return receivedResponse;
}

Result:

{
  "args": {}, 
  "data": "", 
  "files": {}, 
  "form": {
    "<?xml version": "\"1.0\" encoding=\"UTF-8\"?><atesapiorder><auth><login></login><salt>2020-05-19 16:14:2950</salt><passfingerprint></passfingerprint></auth><header><deliveryaddresscode>ZZ05</deliveryaddresscode><buyerorderreference>olo10</buyerorderreference><remarks>opcjonalne uwagi do zam\u00f3wienia</remarks></header><lines></lines></atesapiorder>"
  }, 
  "headers": {
    "Content-Length": "948", 
    "Content-Type": "application/x-www-form-urlencoded", 
    "Host": "httpbin.org", 
  }, 
  "json": null,
  "url": "https://httpbin.org/post"
}

Expected:

{
  "args": {}, 
  "data": "", 
  "files": {}, 
  "form": {
    "xml": "<?xml version='1.0' encoding='UTF-8'?>\n<atesapiorder>\n    <auth>\n        <login></login>\n        <salt></salt>\n        <passfingerprint></passfingerprint>\n    </auth>\n    <header>\n        <deliveryaddresscode>ZZ05</deliveryaddresscode>\n        <buyerorderreference>test101111</buyerorderreference>\n        <remarks>opcjonalne uwagi do zam\u00f3wienia</remarks>\n    </header>\n    <lines></lines>\n</atesapiorder>"
  }, 
  "headers": {
    "Accept": "*/*", 
    "Accept-Encoding": "gzip, deflate, br", 
    "Cache-Control": "no-cache", 
    "Content-Length": "1460", 
    "Content-Type": "application/x-www-form-urlencoded", 
    "Host": "httpbin.org", 
  }, 
  "url": "https://httpbin.org/post"
}
Szczepan
  • 47
  • 5

0 Answers0