Learn to send POST request from AEM to AC for creating recipients using dynamic javascript templates (jssp) which can further open ways of integrating AC to various web technologies. In the end, it would be just GET and POST and parsing the HTTP response.
Upload Binary data as HTTP POST
Below is the ASP.NET C# implementation of Uploading binary data like images as POST request to target URL:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | private bool UploadFile(string PostURL) { try { int contentLength = fileUpload.PostedFile.ContentLength; byte[] data = new byte[contentLength]; fileUpload.PostedFile.InputStream.Read(data, 0, contentLength); // Prepare web request... HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(PostURL); webRequest.Method = "POST"; webRequest.ContentType = "multipart/form-data"; webRequest.ContentLength = data.Length; using (Stream postStream = webRequest.GetRequestStream()) { // Send the data. postStream.Write(data, 0, data.Length); postStream.Close(); } return true; } catch (Exception ex) { //Log exception here... return false; } } |
The binary data so posted can be retrieved on target post URL using below code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | byte[] Image = ReadFully(Request.InputStream); ................. public static byte[] ReadFully(Stream input) { byte[] buffer = new byte[16 * 1024]; using (MemoryStream ms = new MemoryStream()) { int read; while ((read = input.Read(buffer, 0, buffer.Length)) > 0) { ms.Write(buffer, 0, read); } return ms.ToArray(); } } |
This scenario is very handy specifically in establishing communication between two different web technologies. For example: You can POST image …
Continue reading “Upload Binary data as HTTP POST”