Hi,
I have developed a small REST application using OpenEdge 11.6. (see screen shot below) getting the response successfully.
Now, I am using the following code to consume this REST service, but I am getting the StatusCode as 200 and StatusReason "Forbidden". Can anyone help me in resolving this issue?
Any help will be highly appreciable.
Thanks in advance
I have written this code using the following youtube video
/*------------------------------------------------------------------------
File : getProperties.p
Purpose :
Syntax :
Description :
Author(s) :
Created : Wed Mar 30 13:25:00 IST 2016
Notes :
----------------------------------------------------------------------*/
/* *************************** Definitions ************************** */
BLOCK-LEVEL ON ERROR UNDO, THROW.
/* ******************** Preprocessor Definitions ******************** */
/* *************************** Main Block *************************** */
USING OpenEdge.Net.HTTP.*.
USING OpenEdge.Net.URI.
USING Progress.Json.ObjectModel.JsonObject.
LOG-MANAGER:LOGFILE-NAME = "C:/OpenEdge/WRK/request-log1.txt".
LOG-MANAGER:LOGGING-LEVEL=5.
DEFINE VARIABLE oClient AS IHTTPClient NO-UNDO.
DEFINE VARIABLE oURI AS URI NO-UNDO.
DEFINE VARIABLE oCredentials AS Credentials NO-UNDO.
DEFINE VARIABLE oRequest AS IHttpRequest NO-UNDO.
DEFINE VARIABLE oResponse AS IHttpResponse NO-UNDO.
DEFINE VARIABLE oCookies AS Cookie NO-UNDO.
oClient = ClientBuilder:Build():KeepCookies(CookieJarBuilder:Build():CookieJar):Client.
oURI = NEW URI('http', 'ctsc00520904801.cts.com', 8810).
oURI:Path = '/myOEABLWebAppService/postCustomer/'.
oCredentials = NEW Credentials('Tomcat Application', 'tomcat', 'tomcat').
oRequest = RequestBuilder:Build('POST', oURI)
:UsingCredentials(oCredentials)
:usingBasicAuthentication(oCredentials)
:acceptJson()
:Request.
oResponse = ResponseBuilder:Build():Response.
oClient:execute(oRequest, oResponse).
oResponse = ClientBuilder:Build():Client:Execute(oRequest).
MESSAGE STRING(oResponse:StatusCode) SKIP
oResponse:StatusReason
VIEW-AS ALERT-BOX.
/*
IF oResponse:StatusCode <> 200 THEN
DISPLAY "Request Error" + String(oResponse:StatusCode).
ELSE
CAST(oResponse:entity, JsonObject):WriteFile('response.json', TRUE).
*/
LOG FILE Content:
[16/08/31@23:25:58.615+2300] P-005440 T-011016 1 4GL HTTPCLIB REQUEST: FILE= C:\OpenEdge\WRK\request-raw.txt
[16/08/31@23:25:58.618+2300] P-005440 T-011016 1 4GL HTTPCLIB RESPONSE: FILE= C:\OpenEdge\WRK\response-data-received.txt
[16/08/31@23:25:58.620+2300] P-005440 T-011016 1 4GL SOCKET CONNECT: -H ctsc00520904801.cts.com -S 8810
[16/08/31@23:25:58.621+2300] P-005440 T-011016 1 4GL SOCKET CONNECT: TIME(ms)=1
[16/08/31@23:25:58.621+2300] P-005440 T-011016 1 4GL SOCKET WRITE: TIME(ms)=0
[16/08/31@23:25:58.621+2300] P-005440 T-011016 1 4GL SOCKET WRITE: SIZE(b)=252
[16/08/31@23:25:58.623+2300] P-005440 T-011016 1 4GL SOCKET READ: AVAIL(b)=1049
[16/08/31@23:25:58.623+2300] P-005440 T-011016 1 4GL SOCKET READ: TIME(ms)=0
[16/08/31@23:25:58.623+2300] P-005440 T-011016 1 4GL SOCKET READ: SIZE(b)=1049
[16/08/31@23:25:58.623+2300] P-005440 T-011016 1 4GL HTTPCLIB CHUNK: NUM= 1
[16/08/31@23:25:58.623+2300] P-005440 T-011016 1 4GL HTTPCLIB CHUNK: SIZE(b)= 8192
[16/08/31@23:25:58.623+2300] P-005440 T-011016 1 4GL HTTPCLIB CHUNK: READ(b)= 1049
[16/08/31@23:25:58.668+2300] P-005440 T-011016 1 4GL SOCKET READ: COMPLETE= yes
[16/08/31@23:25:58.668+2300] P-005440 T-011016 1 4GL SOCKET READ: ERROR= ?
[16/08/31@23:25:58.668+2300] P-005440 T-011016 1 4GL SOCKET READ: TOTAL TIME(ms)=45
[16/08/31@23:25:58.671+2300] P-005440 T-011016 1 4GL HTTPCLIB EXTRACT ENTITY: ERROR = Progress.Json.JsonParserError_1318
[16/08/31@23:25:58.671+2300] P-005440 T-011016 1 4GL HTTPCLIB EXTRACT ENTITY: Response entity contains raw message body
The reason for not showing status 200, because of screen shot not taken appropriately. Below is the scree shot having status code.
I am not sure where exactly I have to provide the input JSON.
Output of response-data-received.txt
HTTP/1.1 403 Forbidden
Server: Apache-Coyote/1.1
Set-Cookie: JSESSIONID=50489B30B117FE724C31F05E5012AE18BBDE30DEFCB7.oepas1; Path=/; HttpOnly
Content-Type: application/json;charset=UTF-8
Content-Length: 798
Date: Wed, 31 Aug 2016 17:55:58 GMT
{
"error_code": 403
, "status_text": "403 - Forbidden: the server refuses to fullfill the client's request - POST /myOEABLWebAppService/postCustomer/"
, "error_details": {
"remote_user": "null"
, "http_session": "50489B30B117FE724C31F05E5012AE18BBDE30DEFCB7.oepas1"
, "user_principal": "null"
, "url_scheme": "http"
, "remote_addr": "10.242.244.47"
, "server_name": "ctsc00520904801.cts.com"
, "product_type": "dev"
, "http_status": 403
, "error_detail": "Access Denied"
}
, "debug_details": {
"http_method": "POST"
, "web_application": "/ROOT"
, "transport": "jsp"
, "request_url": "/myOEABLWebAppService/postCustomer/"
, "path_info": "null"
, "servlet": "jsp"
, "uri": "/myOEABLWebAppService/postCustomer/"
, "exception_class": ""
, "exception_message": ""
, "exception_stack_trace":
}
}
I am getting response now . The issue was
1) the URI Path was wrong. It should be
oURI:Path = '/rest/myOEABLWebAppService/postCustomer/'.
2) I was not able to pass the input json.
Now the updated code looks like below :
/*------------------------------------------------------------------------
File : getProperties.p
Purpose :
Syntax :
Description :
Author(s) :
Created : Wed Mar 30 13:25:00 IST 2016
Notes :
----------------------------------------------------------------------*/
/* *************************** Definitions ************************** */
BLOCK-LEVEL ON ERROR UNDO, THROW.
/* ******************** Preprocessor Definitions ******************** */
/* *************************** Main Block *************************** */
USING OpenEdge.Net.HTTP.*.
USING OpenEdge.Net.URI.
USING Progress.Json.ObjectModel.JsonObject.
USING Progress.Json.ObjectModel.JsonArray.
LOG-MANAGER:LOGFILE-NAME = "C:/OpenEdge/WRK/request-log1.txt".
LOG-MANAGER:LOGGING-LEVEL=5.
DEFINE VARIABLE oClient AS IHTTPClient NO-UNDO.
DEFINE VARIABLE oURI AS URI NO-UNDO.
DEFINE VARIABLE oCredentials AS Credentials NO-UNDO.
DEFINE VARIABLE oRequest AS IHttpRequest NO-UNDO.
DEFINE VARIABLE oResponse AS IHttpResponse NO-UNDO.
DEFINE VARIABLE oCookies AS Cookie NO-UNDO.
DEFINE VARIABLE vlcRequestData AS LONGCHAR NO-UNDO.
DEFINE VARIABLE oJsonParam AS JsonObject NO-UNDO.
DEFINE VARIABLE oJsonParam1 AS JsonObject NO-UNDO.
DEFINE VARIABLE oJsonDataArr AS JsonArray NO-UNDO.
oClient = ClientBuilder:Build():KeepCookies(CookieJarBuilder:Build():CookieJar):Client.
oURI = NEW URI('http', 'ctsc00520904801.cts.com', 8810).
/*oURI:Path = '/myOEABLWebApp/rest/myOEABLWebAppService/postCustomer/'.*/
/*oURI:Path = '/myOEABLWebApp/rest/WebApp2Service/postCustomer/'.*/
oURI:Path = '/rest/myOEABLWebAppService/postCustomer'.
/*oCredentials = NEW Credentials('Tomcat Application', 'tomcat', 'tomcat').*/
oJsonParam = NEW Progress.Json.ObjectModel.JsonObject().
oJsonParam:add("userId" , "ahussain").
oJsonParam:add("password" , "P@ssw0rD").
oJsonParam:add("whereClause" , " Customer.CustNum <= 5 ").
oJsonParam:add("className","genericData").
oJsonParam:add("methodName","getTableBasicInfo").
oJsonParam:add("tableName","Customer").
oJsonParam:add("schemaInfoRequired" , "No").
oJsonParam:add("includeFields" , "").
oJsonParam:add("excludeFields" , "").
oJsonParam:add("programName" , "").
oJsonParam:add("programParameters" , "").
oJsonParam:add("ID","Customer").
oJsonParam1 = NEW Progress.Json.ObjectModel.JsonObject().
oJsonParam1:add("Parameters", oJsonParam).
oJsonDataArr = NEW Progress.Json.ObjectModel.JsonArray().
oJsonParam1:add("Data", oJsonDataArr).
oRequest = RequestBuilder:Build('POST', oURI)
:AddJsonData(oJsonParam1)
:ContentType('application/json')
/*:UsingCredentials(oCredentials)
:usingBasicAuthentication(oCredentials)*/
:acceptJson()
:REQUEST.
oResponse = ResponseBuilder:Build():Response.
oClient:execute(oRequest, oResponse).
oResponse = ClientBuilder:Build():Client:Execute(oRequest).
MESSAGE STRING(oResponse:StatusCode) SKIP
STRING(oResponse:StatusReason, "x(30)")
VIEW-AS ALERT-BOX.
IF oResponse:StatusCode <> 200 THEN
DISPLAY "Request Error" + String(oResponse:StatusCode).
ELSE
CAST(oResponse:entity, JsonObject):WriteFile('c:\OpenEdge\WRK\response.json', TRUE).