Xbase++ & eXpress++ & xb2net who use ?

This forum is for eXpress++ general support.
Message
Author
skiman
Posts: 1185
Joined: Thu Jan 28, 2010 1:22 am
Location: Sijsele, Belgium
Contact:

Re: Xbase++ & eXpress++ & xb2net who use ?

#11 Post by skiman »

Hi,

Hereby the code of my RESTHANDLER;

I'm working with Json Web Tokens for authorization. If someone sends a request, the token is validated and if it is correct, the function (endpoint) is called. The response headers are added to solve the CORS problem. (Cross-Origin Resource Sharing ).

Code: Select all

STATIC PROCEDURE RESTHandler( oThread )
*************************************************
   Local cWebFunction , rec
   Local cAction := oThread:HTTPRequest:Path() , bFunction 
   Local cCommand := oThread:httpRequest:command , cEndpoint := "" , aUrl := {}
   Local cOrigin := oThread:HTTPRequest:origin()

	if empty(cOrigin)
		cOrigin := "*"
	endif
	oThread:HTTPResponse:setheader('Access-Control-Allow-Origin', cOrigin)
	oThread:HTTPResponse:setheader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS' )
	oThread:HTTPResponse:setheader('Access-Control-Allow-Headers', '*')    // 'Origin, Content-Type, X-Auth-Token, Authorization')
   ErrorLog("Actie: "+ cAction)

	aUrl := dc_tokenarray(substr(cAction,2),"/")
	if len(aUrl) < 2
		return 
	endif
	if upper(aUrl[2]) <> "LOGIN"
		if !jwt_token("check")
			rec:=json():new()
			rec:error := "Token is expired."
			sendjson(rec)
			return
		endif
	endif
	cEndpoint := "REST_"+aUrl[2]

	cWebFunction := "{|cCommand,aUrl| " + cEndpoint+"(cCommand,aUrl) }"

    if IsFunction(cEndpoint)    
        // macro compile and execute the requested function
		bFunction := &(cWebFunction)
	    eval(bFunction,cCommand,aUrl)
    else
        oThread:NotFound()
    endif
Return
Best regards,

Chris.
www.aboservice.be

patito
Posts: 121
Joined: Tue Aug 31, 2010 9:01 pm

Re: Xbase++ & eXpress++ & xb2net who use ?

#12 Post by patito »

Hi Chris

What is the way you return Json's result from the controller MVC?
? ActionResult or JsonResult

Best Regard
Héctor Pezoa

skiman
Posts: 1185
Joined: Thu Jan 28, 2010 1:22 am
Location: Sijsele, Belgium
Contact:

Re: Xbase++ & eXpress++ & xb2net who use ?

#13 Post by skiman »

Hi Hector,

I don't understand your question about ActionResult or JsonResult. This is what i return:
cContent is the variable with the json string.

Code: Select all

 
o:httpResponse:contenttype := 'application/json'
if len(cContent) < 10000 .or. lFlat
	o:httpResponse:ContentEncoding("identity") 
	o:httpresponse:CompressLevel = 0  
	o:httpResponse:content :=  cContent   
else
	o:httpResponse:ContentEncoding( "deflate" )  
	o:httpResponse:content :=  xbZCompress( cContent, 4, @nErr, .t. )  
endif
Best regards,

Chris.
www.aboservice.be

patito
Posts: 121
Joined: Tue Aug 31, 2010 9:01 pm

Re: Xbase++ & eXpress++ & xb2net who use ?

#14 Post by patito »

Hi Chris
Thank you very much for your prompt response
By your code I see that you have taken a different path, but
maybe you get to rome
This week I will continue with outh2 and I hope to make it.

Best Regard
Héctor Pezoa

...Client

oHttp := TServerXMLHTTPRequest():New()
oHttp:Open("POST","http://127.0.0.1:8080/v1/Client",.f.)
oHttp:SetReQuestHeader( 'Content-Type', 'application/json;charset=UTF-8')
oHttp:Send(cJString) // Send body (MODO RAW )
cResponse := oHttp:responseText
nStatus := oHttp:status


..Server

METHOD BaseController:controller( cID )
Local o := ThreadObject()
local n , jso
IF !::oService:getError()

DO CASE

CASE ::R_METHOD == "GET" .AND. Empty( cId)
jso := ::find()

CASE ::R_METHOD == "GET" .AND. !Empty( cId)
jso := ::oService:getId( cId )
CASE ::R_METHOD == "DELETE" .AND. !Empty( cId)
jso := ::oService:DeletedId( cId )

CASE ::R_METHOD == "POST"
IF ::check_content_type()
jso := ::oService:Create( json_unserialize(::R_BODY ))
else
o:HTTPResponse:ContentType( "text/html" )
o:HTTPResponse:StatusCode := 415
o:HTTPResponse:Content := 'Error'
::oService:lError := .T.
ENDIF

CASE ::R_METHOD == "PUT"
IF ::check_content_type() // 'application/json'
jso := ::oService:Modify(json_unserialize(::R_BODY ))
ELSE
o:HTTPResponse:ContentType( "text/html" )
o:HTTPResponse:StatusCode := 415
o:HTTPResponse:Content := 'Error'
::oService:lError := .T.
ENDIF

END CASE
ENDIF

If ::oService:lError := .f.
o:HTTPResponse:ContentType('application/json')
o:HTTPResponse:StatusCode := ::oService:nCodeStatus
o:HTTPResponse:Content := jso
endif

Post Reply