I am trying to hit a third party API (cannot share, confidential) and they expect the following json as input:
{
PFID: “abc”,
CY: 2015,
AZs: [
{ AZN: “AZ1”,
x: <bytes>, y: <bytes>, x: <bytes>
},
{ AZN: “AZ2”,
x: <bytes>, y: <bytes>, z: <bytes>
}
]
}
I am using HTTPClient to submit a request to this WebAPI. Following is the snippet for that:
public void AddApplication(AZR request)
{
HttpClient client = new HttpClient();
client.BaseAddress = new Uri(url);
client.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Add("authorization","mybearertoken");
innerJson = Json.JsonParser.Serialize<AppZoneRequest>(request); // <=== coming perfectly till here. json returns a perfect json with in bytes flowing in a perfect hierarchy
HttpContent content = new StringContent(innerJson, Encoding.UTF8, "application/json"); // <==== my best guess is problem is here
HttpResponseMessage response = client.PostAsync(apiURL, content).Result;
return response;
}
public class AZR
{
public string PFID { get; set; }
public int CY { get; set; }
public List<xyz> AZs { get; set; }
}
public class xyz
{
public string AZN { get; set; }
public byte[] x { get; set; }
public byte[] y { get; set; }
public byte[] z { get; set; }
}
When I post the request, it returns to me the Status 500, which suggests internal server error. I understand that you cannot serialize Byte Array in a string Content, and we need to use ByteArrayContent, and if you have both, then use HttpMultipartFormDataContent. But here, the hierarchy is such, that I have string, int, and an array of objects and the object within it contains string, byte[], byte[], byte[]. Now, I am really unable to find a way to submit a post request with such. Any help?