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

sjhl
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.
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.
0
Best Answer
-
Hey sjhl,
Please refer to this URL it may help you.
kauffmann.nl/index.php/2015/11/26/web-services-examples-part-1-the-basic-pattern/
Thanks
Blog - rockwithnav.wordpress.comThanks
Blog - rockwithnav.wordpress.com/
Twitter - https://twitter.com/RockwithNav
Facebook - https://facebook.com/rockwithnav/1
Answers
-
Hey sjhl,
Please refer to this URL it may help you.
kauffmann.nl/index.php/2015/11/26/web-services-examples-part-1-the-basic-pattern/
Thanks
Blog - rockwithnav.wordpress.comThanks
Blog - rockwithnav.wordpress.com/
Twitter - https://twitter.com/RockwithNav
Facebook - https://facebook.com/rockwithnav/1 -
Maybe here you can find what you want: http://www.dynamics.is/?p=23031
-
RockWithNAV hello at first I was like I have looked through this website blog already but I looked at it again. This helped me a lot. I had to do 'POST' and get some help from other resources but at the end this helped. Now email is sent! thanks a lot!!!0
-
Glad to hear this. As you know in NAV Integrations People till now were just using SOAP services but JSON/Restful Services has now too been introduced so sometimes a small mistake in this becomes a nightmare to find.
Thanks
Blog - rockwithnav.wordpress.com
Thanks
Blog - rockwithnav.wordpress.com/
Twitter - https://twitter.com/RockwithNav
Facebook - https://facebook.com/rockwithnav/0
Categories
- All Categories
- 73 General
- 73 Announcements
- 66.6K Microsoft Dynamics NAV
- 18.7K NAV Three Tier
- 38.4K NAV/Navision Classic Client
- 3.6K Navision Attain
- 2.4K Navision Financials
- 116 Navision DOS
- 851 Navision e-Commerce
- 1K NAV Tips & Tricks
- 772 NAV Dutch speaking only
- 617 NAV Courses, Exams & Certification
- 2K Microsoft Dynamics-Other
- 1.5K Dynamics AX
- 320 Dynamics CRM
- 111 Dynamics GP
- 10 Dynamics SL
- 1.5K Other
- 990 SQL General
- 383 SQL Performance
- 34 SQL Tips & Tricks
- 35 Design Patterns (General & Best Practices)
- 1 Architectural Patterns
- 10 Design Patterns
- 5 Implementation Patterns
- 53 3rd Party Products, Services & Events
- 1.6K General
- 1.1K General Chat
- 1.6K Website
- 83 Testing
- 1.2K Download section
- 23 How Tos section
- 252 Feedback
- 12 NAV TechDays 2013 Sessions
- 13 NAV TechDays 2012 Sessions