Complete CodeUnit:
OBJECT Codeunit 90002 WebService Connection
{
OBJECT-PROPERTIES
{
Date=11-01-12;
Time=15:05:21;
Modified=Yes;
Version List=;
}
PROPERTIES
{
OnRun=BEGIN
CREATE(XmlIn);
CREATE(XmlOut);
// Populating XmlIn document for test purposes
LoadTestXml();
// Invoke method
Send();
// Save result to disc
XmlOut.save('C:\Temp\XML\response.xml');
END;
}
CODE
{
VAR
XmlIn@1000000004 : Automation "{F5078F18-C551-11D3-89B9-0000F81FE221} 6.0:{F6D90F11-9C73-11D3-B32E-00C04F990BB4}:'Microsoft XML, v6.0'.DOMDocument";
XmlOut@1000000003 : Automation "{F5078F18-C551-11D3-89B9-0000F81FE221} 6.0:{F6D90F11-9C73-11D3-B32E-00C04F990BB4}:'Microsoft XML, v6.0'.DOMDocument";
locautXmlHttp@1160870000 : Automation "{F5078F18-C551-11D3-89B9-0000F81FE221} 6.0:{88D96A0A-F192-11D4-A65F-0040963251E5}:'Microsoft XML, v6.0'.XMLHTTP60";
PROCEDURE Send@1000000004();
BEGIN
// Communication
CREATE(locautXmlHttp);
locautXmlHttp.open('POST','http://localhost:56063/RESTservice.ashx',0);
locautXmlHttp.setRequestHeader('Content-type','text/xml; charset=utf-8');
locautXmlHttp.setRequestHeader('KeepAlive','false');
locautXmlHttp.setRequestHeader('Timeout','5000');
// Send request
locautXmlHttp.send(XmlIn);
// Get response
XmlOut := locautXmlHttp.responseXML;
XmlOut.async := TRUE;
// Show result
MESSAGE(XmlOut.xml);
END;
PROCEDURE LoadTestXml@1160870000();
BEGIN
XmlIn.loadXML(
'<xmltest>' +
'<test>' +
'This document can contain anything. But must not contain xml decleration.' +
'</test>' +
'</xmltest>');
END;
BEGIN
{
Shows how to connect to a REST Web Service
}
END.
}
}
.NET REST webservice implemented as a generic handler (.ashx) nothing fancy:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
namespace WebServiceApplication
{
/// <summary>
/// Summary description for RESTservice
/// </summary>
public class RESTservice : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
string requestMethod = context.Request.HttpMethod; // POST, PUT, GET or DELETE
System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
doc.Load(context.Request.InputStream);
System.IO.Stream stream = context.Request.InputStream;
byte[] bytes = new byte[stream.Length];
stream.Position = 0; // Resetting reading position
stream.Read(bytes, 0, (int)stream.Length);
string data = Encoding.ASCII.GetString(bytes); // this is your string
//context.Response.ContentType = "text/plain";
context.Response.ContentType = "text/xml";
context.Response.Write("<result>Hello World</result>");
context.Response.End();
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
Hi, I am getting an error while using your codeunit
SvarSlet"Error 405: Method not allowed"
What could be its resolution?
Hi Dhan Raj
SvarSletIt's an error message from the service you're trying to call informing you that you are not allowed to post data to the service (I'm assuming you are using the code as posted above).
A little info on the error here: http://www.checkupdown.com/status/E405.html
Hope it helps.
/Simon
Hi,
SvarSletHow we can call JSON rest API with JSON request and response from NAV 2009.
Please advice
Hey Ahmad,
SletHave you actually done this in the end? Called a Rest API from NAV 2009?
Cheers