I have tried sending data to an API through C#. It is my first time doing so and I unfortunately only get an error 400 when making my request. I followed the exemple in this post to make my request, (using method C) but to no avail. Here's the code I have, which follow the exemple, save for creating the XML itself. That said, for the purpose of my test, I extracted the result of my serialization into a variable. Her's the code:
var serializerRequest = new XmlSerializer(typeof(Request.ImportRequest));
var stream = new MemoryStream();
serializerRequest.Serialize(stream, importRequest);
stream.Flush();
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
byte[] bytes;
bytes = System.Text.Encoding.ASCII.GetBytes(new string(Encoding.UTF8.GetChars(stream.GetBuffer())));
String test = new string(Encoding.UTF8.GetChars(stream.GetBuffer()));
request.ContentType = "text/xml; encoding='utf-8'";
request.ContentLength = bytes.Length;
request.Method = "POST";
using (Stream requestStream = request.GetRequestStream())
{
requestStream.Write(bytes, 0, bytes.Length);
}
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Console.WriteLine(response.ToString());
if (response.StatusCode == HttpStatusCode.OK)
{
Stream responseStream = response.GetResponseStream();
string responseStr = new StreamReader(responseStream).ReadToEnd();
XmlSerializer serializerResponse = new XmlSerializer(typeof(Response.Result));
using (TextReader reader = new StringReader(new string(Encoding.UTF8.GetChars(stream.GetBuffer()))))
{
Response.Result result = (Response.Result)serializerResponse.Deserialize(reader);
return result;
}
}
return null;
And here's the XML that is being sent.
<?xml version="1.0"?>
<ImportRequest xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Login>
<Username>XXXXXXX</Username>
<Password>PPPPPPPPP</Password>
</Login>
<Test>True</Test>
<ClientID>9999</ClientID>
<Reference>8888888</Reference>
<ShipTo>
<Name>CLIENTNAME</Name>
<Address1>ADDRESS</Address1>
<Address2 />
<City>NAMUR</City>
<State>BE</State>
<PostalCode>5000</PostalCode>
<Country>BELGIUM</Country>
<Phone />
</ShipTo>
<ShippingLane>
<Region>LandMark Brussels</Region>
</ShippingLane>
<ShipMethod>LGINTSTD</ShipMethod>
<ItemsCurrency>EUR</ItemsCurrency>
<ProduceLabel>true</ProduceLabel>
<LabelFormat>ZPL</LabelFormat>
<LabelEncoding>ZPL</LabelEncoding>
<VendorInformation>
<VendorName>XXXXXXXXX</VendorName>
<VendorAddress1>ADDRESS</VendorAddress1>
<VendorCity>NAMUR</VendorCity>
<VendorState>BE</VendorState>
<VendorPostalCode>5000</VendorPostalCode>
<VendorCountry>Belgium</VendorCountry>
<VendorBusinessNumber>+3255555555</VendorBusinessNumber>
</VendorInformation>
<Packages>
<Package>
<Weight>10</Weight>
<DimensionsUnit>Kg</DimensionsUnit>
</Package>
</Packages>
</ImportRequest>
What bothers me the most is that when I put the XML into postman it work without an itch. So I'm convinced my XML is working, and I followed the guide on how to make webrequest, but for some reason it doesn't seem to work. I get an error 400 so I'm reaching the server. It's certainly an issue with my code, but I can't find it. I would appreciate some help. Thanks and have a nice day.