0

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?

Vikas
  • 795
  • 1
  • 4
  • 20
  • Are you sure they meant a `byte[]` and not a base64 encoded string? – Yuval Itzchakov Nov 19 '15 at 07:28
  • the first code block is picked up exactly from their documentation. Ofcourse, have changed the variable names. But this is what they say we need to send. I am open to try anything, you think I should rather send Base64 enceded string instead of byte? FYI, the byte is actually File Bytes – Vikas Nov 19 '15 at 07:30
  • tried, didn't work. Any more suggestions? – Vikas Nov 19 '15 at 07:40
  • 2
    I think your best bet is to contact that API provider an ask them exactly what they meant. – Yuval Itzchakov Nov 19 '15 at 07:47
  • can not send byte[] directly in json. just one solution exists there and it's using base64 string. [how to use bas64 encoding]http://stackoverflow.com/questions/20706783/put-byte-array-to-json-and-vice-versa – hvojdani Nov 19 '15 at 08:00
  • @Yuval, yeah I will do that. @ tomcater, I tried that too, but still didn't work. I would go to the API Vendor which I didn't have time to go to. Anyway, thanks for the help. I will post the answer once I get to know the problem. – Vikas Nov 19 '15 at 08:18
  • so I checked with the vendor. Their Post Method is this: public void Post(string PFID, int CY, AZR [] AZs). Suggestions? – Vikas Nov 19 '15 at 08:55
  • @Vikas Did you ended up finding the solution? – Mario Galván Sep 26 '18 at 23:08

0 Answers0