I'm trying to POST a xml body to an api. this is what I have.
private HttpClient _client;
private HttpResponseMessage BaseHttpCallForStringData(string authorizationString, string targetUrl, string method, string contentType, string data)
{
logger.LogInformation("BaseHttpCall: " + "start");
response = null;
_client.DefaultRequestHeaders.Clear();
if (!string.IsNullOrEmpty(contentType))
{
_client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(contentType));
}
logger.LogInformation("Calling service: " + targetUrl);
if (authorizationString != null)
{
_client.DefaultRequestHeaders.Add("X-API-KEY", authorizationString);
}
try
{
HttpContent _Body = new StringContent("");
if (!string.IsNullOrEmpty(contentType))
{
_Body.Headers.ContentType = new MediaTypeHeaderValue(contentType);
}
if (data != null)
{
string objData = data;
_Body = new StringContent(objData);
}
response = _client.PostAsync(targetUrl, _Body).Result;
}
catch (Exception e)
{
response = null;
logger.LogError("Error: " + e.StackTrace);
logger.LogError("Log E: ");
logger.LogError(null, e, "error");
}
return response;
}
Double checked the API-KEY, username and password for the api. They are correct. Tried in Postman, returns status 200. I'm kinda confused which one of credentials are wrong, as they all have the correct values when debugging. Thanks for helping guys.
Edit#1
POST https://api30-3.infinite-convergence.com/soap: {
"Network": {
"addresses": {
"local": {
"address": "192.168.10.142",
"family": "IPv4",
"port": 55555
},
"remote": {
"address": "52.194.115.221",
"family": "IPv4",
"port": 443
}
},
"tls": {
"reused": false,
"authorized": true,
"authorizationError": null,
"cipher": {
"name": "ECDHE-RSA-AES128-GCM-SHA256",
"standardName": "TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256",
"version": "TLSv1/SSLv3"
},
"protocol": "TLSv1.2",
"ephemeralKeyInfo": {},
"peerCertificate": {
"subject": {
"commonName": "*.infinite-convergence.com",
"alternativeNames": "DNS:*.infinite-convergence.com, DNS:infinite-convergence.com"
},
"issuer": {
"country": "GB",
"stateOrProvince": "Greater Manchester",
"locality": "Salford",
"organization": "Sectigo Limited",
"commonName": "Sectigo RSA Domain Validation Secure Server CA"
},
"validFrom": "May 10 00:00:00 2022 GMT",
"validTo": "Jun 10 23:59:59 2023 GMT",
"fingerprint": "3F:44:D6:C8:EC:42:05:E9:EE:8F:72:4B:1D:D3:49:7A:7E:31:B1:F8",
"serialNumber": "850bed13e44777cce777b77779bafee5"
}
}
},
"Request Headers": {
"x-api-key": "API Key same as the c# code",
"cache-control": "no-cache",
"postman-token": "def36a21-c850-4a7f-8b03-e7c11ee135b4",
"host": "api30-3.infinite-convergence.com",
"content-length": "877"
},
"Request Body": "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" \r\nxmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" \r\nxmlns:soapenc=\"http://schemas.xmlsoap.org/soap/encoding/\" \r\nxmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\r\n<soap:Body soap:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">\r\n<SendSMS xmlns=\"http://infinite.com/sms\">\r\n<authid xsi:type=\"xsd:string\">same as well</authid>\r\n<authcode xsi:type=\"xsd:string\">same</authcode>\r\n<fieldcodes xsi:type=\"xsd:string\">{name},%time,%code</fieldcodes>\r\n<!-- <source xsi:type=\"xsd:string\">123456</source> -->\r\n<destination xsi:type=\"xsd:string\">\r\n6590891828;j; 1234;\r\n6592289062;e\r\n</destination>\r\n<message xsi:type=\"xsd:string\">Hi {name}, your order is ready for %time\r\npickup. Use code 6354 for quicker service. </message>\r\n</SendSMS>\r\n</soap:Body>\r\n</soap:Envelope>",
"Response Headers": {
"date": "Sat, 28 May 2022 02:28:56 GMT",
"content-type": "text/xml; charset=utf-8",
"content-length": "608",
"connection": "keep-alive",
"server": "Apache"
},
"Response Body": "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soapenc=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:tns=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:types=\"http://infinite.com/sms//encodedTypes\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n<soap:Body soap:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">\n<SendSMSResponse xmlns=\"http://infinite.com/sms\">\n<SendSMSResult xsi:type=\"xsd:string\">success</SendSMSResult>\n</SendSMSResponse>\n</soap:Body>\n</soap:Envelope>\n"
}
Solution
Used a string instead of using a StringBuilder. Will try to figure out why this happens and update on this question again.