trying to build http adapter from ajax request

161 Views Asked by At

i'm trying to build http adapter with token authorization form ajax request but get 401 error

Status Code:strong text 401 Unauthorized missing_authorization

$.ajax({
           type: "POST",   
           url: "https://abcd",
           data: JSON.stringify({ "template": 1 }),
           headers: { "Authorization": "xxxx", "Accept": "application/json", 
"Content-Type": "application/json" }
});

function My_adapter() {
   path = '/xxx';
   var input = {
       method : 'post',
       path : path,
       returnedContentType : 'json',
      headers: {'Content-type':'application/json', 
'Accept':'application/json', 'Authorization':'Token XXXXX'},             
       parameters: JSON.stringify({ "template": 1 }),            
   };
   var result=WL.Server.invokeHttp(input);  
   return result;

}

tnx for your help, sahar

1

There are 1 best solutions below

4
Vivin K On

The error message you see is expected. The client side code you posted shows that you are attempting to invoke MFP server outside of MFP client SDK ( jQuery ajax call). This call does not carry all the required information to the server and server sends the "missing_authorization" message as a result.

If you wish to invoke an adapter, use WLResourceRequest API provided by MFP client SDK. This takes care of handling the authentication handshake with MFP server. More details on the API usage here.