-1

As per my understanding, the service-objects that we are registering are invoked by kinvey collections only. Is there any way to test that locally ?

I have written below code.

var sdk = require('kinvey-flex-sdk');

const PORT = 8080;

sdk.service({ host: 'localhost', port: PORT }, function (err, flex) {

 const data = flex.data; 

 function authentication(request, complete, modules) {
       console.log("Inside...");
       complete().setBody("Authenticated");
 };

const widgets = data.serviceObject('widgets');

widgets.onInsert(authentication);

});

And my doubt is authentication function is not triggered for the POST request http://localhost:8080/widgets {}..

Can u please help me in resolving that ?

Thanks in advance..

Brian
  • 3,850
  • 3
  • 21
  • 37

2 Answers2

0

You can run the service locally by just running "node index.js". It'll start a listener you can approach with e.g. Postman. Each local test requires a "POST" to a certain routes. Testing routes are documented here: http://devcenter.kinvey.com/ios/guides/flex-services#installation

Also see http://devcenter.kinvey.com/guides/flex-services#testing-locally for the necessary headers you need to set on each request to get the correct context and permission

Ivo Janssen
  • 476
  • 3
  • 9
  • thanks for the commnet. I have added my code to the question. can u please take a look and help me if possible ? – Dhilip Kumar Mar 17 '17 at 06:38
  • What are the specific headers and postbody you are using? When I run your code, I do get the "Inside" log line. But the code doesn't complete because the completion handler is not fully correct. You must always end with complete. and complete.. In your case, you can do: "complete.setBody({some json here}).ok().done()". Also, one typcially returns js-objects (translated to json) in the setBody, not just a string. – Ivo Janssen Mar 22 '17 at 13:43
  • I am using "Content-Type" as "application/json" and "x-auth-key" as "kinveyTest" in Postman Headers. Do i need to add some other header for testing locally ? And i am hitting with "http://localhost:8080/:widgets/" (POST method) – Dhilip Kumar Apr 20 '17 at 06:25
  • You must always end with complete. and complete.. In your case, you can do: "complete.setBody({some json here}).ok().done()". You are forgetting the ok and done parts. – Ivo Janssen Apr 25 '17 at 19:28
0

You need to pass following headers :

Content-Type:application/json
Authorization:Basic <basicAuth>
X-Kinvey-App-Metadata:{ "_id" : "<kinveyAppId>", "appsecret" : "<kinveyAppSecret>", "mastersecret" : "<kinveyMasterSecret>", "baasUrl" : "<kinveyBaseURL>" }
X-Kinvey-Original-Request-Headers:{ "x-kinvey-api-version" : "<KinveyApiVersion>", "authorization" : "Basic <basicAuth>", "x-kinvey-client-app-version" : "<KinveyClientAppVersion>"}
X-Kinvey-Username:<username>
X-Kinvey-User-Id:<userid>

If you're working with flex function :

http://localhost:<port>/_flexFunctions/<flexFunctionName>
Hiren Makwana
  • 1,976
  • 2
  • 13
  • 28