Passing a Json as text into stream , and getting the reponse from Mandrill API

sjhlsjhl Member Posts: 2
Hello, I have been struggling to get response from the Mandrill API. I simply want to send a Json formatted in string and pass it on webservice to get the response. While testing in SOAP UI I was able to pass the text and get response but I want to be able to do this in Dynamic NAV.

right now my code looks something like this. Just a simple prototype.

RequestURL := 'https://mandrillapp.com/api/1.0/messages/send.xml?key=xxxxxxxxx;
//send.xml means I will get reponse in xml format
//but the API only accepts json format not xml

JsonText += '{"key": "example key",';
JsonText += '"message": {';
JsonText += '"html": "example html",';
JsonText += '"subject": "example subject",';
JsonText += '"from_email": "message.from_email@example.com",';
JsonText += '"to":[ {"email": "recipient_email",';
JsonText += '"name": "recipient_name"}';
JsonText += '],"metadata":';
JsonText += '{"user_id": "45829",';
JsonText += '"location_id": "111"}}}';

WebRequest := WebRequest.HttpWebRequest;
WebRequest := WebRequest.Create(RequestURL);
WebRequest.Method := 'POST';
;
UTF8Encoding := UTF8Encoding.UTF8Encoding;
WebRequest.Headers.Add('SOAPAction','https://mandrillapp.com/api/1.0/messages/send.xml?key=xxxxxxx');
WebRequest.ContentLength := UTF8Encoding.GetBytes(JsonText).Length;
WebRequest.Host := 'mandrillapp.com';
WebRequest.Connection:= 'KeepAlive';


RequestStream := WebRequest.GetRequestStream;
MESSAGE(FORMAT(UTF8Encoding.GetBytes(JsonText)));
RequestStream.Write(UTF8Encoding.GetBytes(JsonText),0,WebRequest.ContentLength);
RequestStream.Close;

WebResponse := WebRequest.GetResponse;
StreamReader := StreamReader.StreamReader(WebResponse.GetResponseStream);
Response := StreamReader.ReadToEnd;

IF Response = '' THEN BEGIN
MESSAGE('no response');
END ELSE BEGIN
MESSAGE('Response:' + Response);
END;

Note(even if the json text don't have variables, there will be response from MandrillAPI, saying that API key is invalid, etc) even if I just get that response it is good. I can work on editing the variables later.)


Can anyone give me pointer please?
Thank You.

Best Answer

Answers

Sign In or Register to comment.