DF 2.12 and remote HTTP service works via Postman but not AJAX


#1

Hi everyone

I’ve searched the forum but can’t find anything similar with an answer so thought I’d try a new post.

I’ve set up DF 2.12 and a remote HTTP service to Stripe. This works perfectly when I call the DF service endpoint via Postman or via Curl on the command line, e.g.

curl -X GET \
  'https://api.mydfserver.com/api/v2/stripesandbox/accounts?_dc=1524129954370&offset=1&start=0&limit=25' \
  -H 'X-DreamFactory-API-Key: myDFkey'

Then it correctly returns accounts from Stripe in JSON format.

If I try the same GET using Ajax (plain XMLHttpRequest or JQuery) then I see the request fire in Chrome Tools, it returns 200 OK, but the response is empty / blank.

CORS are set to * - no errors at all. I’ve tried from localhost and also from a web server. Curl on the web server works fine too.

I should say that normal calls to DF work fine from Ajax e.g. I can access my MySQL table through _table endpoint without issues, it’s just when I try and use the HTTP service.

Any ideas if this is a bug or if there is some other restriction with HTTP service?

Thanks


#2

Doing some digging about I can see that if I intercept the “return $response” in /opt/bitnami/apps/dreamfactory/htdocs/vendor/dreamfactory/df-rws/src/Services/RemoteWeb.php processRequest method e.g. print_r($response); exit; instead of returning, then I see the output in the Chrome console for the Ajax request.

Seems like something along the way is deleting the response body.

Will do some more digging


#3

Hi Tim,

This is most curious. Can you enable debugging in the .env file (found in the htdocs folder) and change the APP_LEVEL_LOG=debug and then look at the logs and see if you see anything weird when you hit the endpoint via AJAX? If you hit the endpoint in the admin interface in the API Docs tab, is it rendering a 200 with the proper JSON?

Thanks,

Erik


#4

Hi Erik

Thanks for getting back to me. After enabling APP_LOG_LEVEL=debug I see the following in dreamfactory.log when I trigger the AJAX request (Chrome tools shows 200 OK on the GET):

[2018-04-20 12:13:33] local.INFO: [REQUEST] {“API Version”:“2.0”,“Method”:“GET”,“Service”:“stripesandbox”,“Resource”:“accounts”,“Requestor”:1}
[2018-04-20 12:13:33] local.DEBUG: [REQUEST] {“Parameters”:"{"_dc":“1524226412455”,“offset”:“1”,“start”:“0”,“limit”:“25”}",“API Key”:“c286aca1dd281e392ed96c9c9208edfe7552855c2372eda06b189b24db8feb74”,“JWT”:null}
[2018-04-20 12:13:33] local.DEBUG: API event handled: stripesandbox.get.pre_process
[2018-04-20 12:13:33] local.DEBUG: Outbound HTTP request: GET: https://api.stripe.com/v1/accounts
[2018-04-20 12:13:34] local.DEBUG: API event handled: stripesandbox.get.post_process
[2018-04-20 12:13:34] local.DEBUG: Service event handled: stripesandbox.get
[2018-04-20 12:13:34] local.INFO: [RESPONSE] {“Status Code”:200,“Content-Type”:“application/json”}
[2018-04-20 12:13:34] local.INFO: [RESPONSE] {“Status Code”:200,“Content-Type”:“application/json”}

I go to API Docs and select the service, it says “The service ‘stripesandbox’ has no API docs.”

Thanks
Tim


#5

Thanks Tim for the logs!

With regard to the API Docs, I had not had enough coffee. There wouldn’t be anything that showed up as there is not a native Swagger 3.0 service definition attached to the stripe service.

Can you try this:

https://api.mydfserver.com/test_rest.html

Then use your basic auth creds (your login to your df instance) and see if you can see the output when you access the service? This is a lightweight, stripped down version of your df instance (no angular front end) that works like Postman or Insomnia.

Thanks,

Erik


#6

OK now this is interesting. I get the following JSON error response when using test_rest (standalone Postman still works fine), and the response to indicate a 400 error from Stripe due to invalid parameters, but I am not sure where these are coming from. I am not including any params in my original GET:

{
	"error": {
		"code": 400,
		"context": null,
		"message": "{"error":{"code":"parameter_unknown","doc_url":"https:\\/\\/stripe.com\\/docs\\/error-codes\\/parameter-unknown","message":"Received unknown parameter: _","param":"_","type":"invalid_request_error"}}",
		"trace": [
			"0 /opt/bitnami/apps/dreamfactory/htdocs/vendor/dreamfactory/df-core/src/Components/RestHandler.php(196): DreamFactory\\Core\\Rws\\Services\\RemoteWeb->processRequest()",
			"1 /opt/bitnami/apps/dreamfactory/htdocs/vendor/dreamfactory/df-core/src/Services/BaseRestService.php(146): DreamFactory\\Core\\Components\\RestHandler->handleRequest(Object(DreamFactory\\Core\\Utility\\ServiceRequest), 'accounts')",
			"2 /opt/bitnami/apps/dreamfactory/htdocs/vendor/dreamfactory/df-core/src/Services/ServiceManager.php(496): DreamFactory\\Core\\Services\\BaseRestService->handleRequest(Object(DreamFactory\\Core\\Utility\\ServiceRequest), 'accounts')",
			"3 /opt/bitnami/apps/dreamfactory/htdocs/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php(221): DreamFactory\\Core\\Services\\ServiceManager->handleServiceRequest(Object(DreamFactory\\Core\\Utility\\ServiceRequest), 'stripesandbox', 'accounts', false)",
			"4 /opt/bitnami/apps/dreamfactory/htdocs/vendor/dreamfactory/df-core/src/Http/Controllers/RestController.php(159): Illuminate\\Support\\Facades\\Facade::__callStatic('handleServiceRe...', Array)",
			"5 /opt/bitnami/apps/dreamfactory/htdocs/vendor/dreamfactory/df-core/src/Http/Controllers/RestController.php(118): DreamFactory\\Core\\Http\\Controllers\\RestController->handleServiceRequest(Object(DreamFactory\\Core\\Utility\\ServiceRequest), 'stripesandbox', 'accounts')",
			"6 [internal function]: DreamFactory\\Core\\Http\\Controllers\\RestController->handleVersionedService('v2', 'stripesandbox', 'accounts')",
			"7 /opt/bitnami/apps/dreamfactory/htdocs/vendor/laravel/framework/src/Illuminate/Routing/Controller.php(54): call_user_func_array(Array, Array)",
			"8 /opt/bitnami/apps/dreamfactory/htdocs/vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php(45): Illuminate\\Routing\\Controller->callAction('handleVersioned...', Array)",
			"9 /opt/bitnami/apps/dreamfactory/htdocs/vendor/laravel/framework/src/Illuminate/Routing/Route.php(212): Illuminate\\Routing\\ControllerDispatcher->dispatch(Object(Illuminate\\Routing\\Route), Object(DreamFactory\\Core\\Http\\Controllers\\RestController), 'handleVersioned...')",
			"10 /opt/bitnami/apps/dreamfactory/htdocs/vendor/laravel/framework/src/Illuminate/Routing/Route.php(169): Illuminate\\Routing\\Route->runController()",
			"11 /opt/bitnami/apps/dreamfactory/htdocs/vendor/laravel/framework/src/Illuminate/Routing/Router.php(658): Illuminate\\Routing\\Route->run()",
			"12 /opt/bitnami/apps/dreamfactory/htdocs/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(30): Illuminate\\Routing\\Router->Illuminate\\Routing\\{closure}(Object(Illuminate\\Http\\Request))",
			"13 /opt/bitnami/apps/dreamfactory/htdocs/vendor/dreamfactory/df-core/src/Http/Middleware/AccessCheck.php(62): Illuminate\\Routing\\Pipeline->Illuminate\\Routing\\{closure}(Object(Illuminate\\Http\\Request))",
			"14 /opt/bitnami/apps/dreamfactory/htdocs/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(149): DreamFactory\\Core\\Http\\Middleware\\AccessCheck->handle(Object(Illuminate\\Http\\Request), Object(Closure))",
			"15 /opt/bitnami/apps/dreamfactory/htdocs/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))",
			"16 /opt/bitnami/apps/dreamfactory/htdocs/vendor/dreamfactory/df-core/src/Http/Middleware/AuthCheck.php(232): Illuminate\\Routing\\Pipeline->Illuminate\\Routing\\{closure}(Object(Illuminate\\Http\\Request))",
			"17 /opt/bitnami/apps/dreamfactory/htdocs/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(149): DreamFactory\\Core\\Http\\Middleware\\AuthCheck->handle(Object(Illuminate\\Http\\Request), Object(Closure))",
			"18 /opt/bitnami/apps/dreamfactory/htdocs/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))",
			"19 /opt/bitnami/apps/dreamfactory/htdocs/vendor/dreamfactory/df-core/src/Http/Middleware/VerbOverrides.php(42): Illuminate\\Routing\\Pipeline->Illuminate\\Routing\\{closure}(Object(Illuminate\\Http\\Request))",
			"20 /opt/bitnami/apps/dreamfactory/htdocs/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(149): DreamFactory\\Core\\Http\\Middleware\\VerbOverrides->handle(Object(Illuminate\\Http\\Request), Object(Closure))",
			"21 /opt/bitnami/apps/dreamfactory/htdocs/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))",
			"22 /opt/bitnami/apps/dreamfactory/htdocs/vendor/barryvdh/laravel-cors/src/HandleCors.php(37): Illuminate\\Routing\\Pipeline->Illuminate\\Routing\\{closure}(Object(Illuminate\\Http\\Request))",
			"23 /opt/bitnami/apps/dreamfactory/htdocs/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(149): Barryvdh\\Cors\\HandleCors->handle(Object(Illuminate\\Http\\Request), Object(Closure))",
			"24 /opt/bitnami/apps/dreamfactory/htdocs/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))",
			"25 /opt/bitnami/apps/dreamfactory/htdocs/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(102): Illuminate\\Routing\\Pipeline->Illuminate\\Routing\\{closure}(Object(Illuminate\\Http\\Request))",
			"26 /opt/bitnami/apps/dreamfactory/htdocs/vendor/laravel/framework/src/Illuminate/Routing/Router.php(660): Illuminate\\Pipeline\\Pipeline->then(Object(Closure))",
			"27 /opt/bitnami/apps/dreamfactory/htdocs/vendor/laravel/framework/src/Illuminate/Routing/Router.php(635): Illuminate\\Routing\\Router->runRouteWithinStack(Object(Illuminate\\Routing\\Route), Object(Illuminate\\Http\\Request))",
			"28 /opt/bitnami/apps/dreamfactory/htdocs/vendor/laravel/framework/src/Illuminate/Routing/Router.php(601): Illuminate\\Routing\\Router->runRoute(Object(Illuminate\\Http\\Request), Object(Illuminate\\Routing\\Route))",
			"29 /opt/bitnami/apps/dreamfactory/htdocs/vendor/laravel/framework/src/Illuminate/Routing/Router.php(590): Illuminate\\Routing\\Router->dispatchToRoute(Object(Illuminate\\Http\\Request))",
			"30 /opt/bitnami/apps/dreamfactory/htdocs/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(176): Illuminate\\Routing\\Router->dispatch(Object(Illuminate\\Http\\Request))",
			"31 /opt/bitnami/apps/dreamfactory/htdocs/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(30): Illuminate\\Foundation\\Http\\Kernel->Illuminate\\Foundation\\Http\\{closure}(Object(Illuminate\\Http\\Request))",
			"32 /opt/bitnami/apps/dreamfactory/htdocs/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/ValidatePostSize.php(27): Illuminate\\Routing\\Pipeline->Illuminate\\Routing\\{closure}(Object(Illuminate\\Http\\Request))",
			"33 /opt/bitnami/apps/dreamfactory/htdocs/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(149): Illuminate\\Foundation\\Http\\Middleware\\ValidatePostSize->handle(Object(Illuminate\\Http\\Request), Object(Closure))",
			"34 /opt/bitnami/apps/dreamfactory/htdocs/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))",
			"35 /opt/bitnami/apps/dreamfactory/htdocs/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/CheckForMaintenanceMode.php(46): Illuminate\\Routing\\Pipeline->Illuminate\\Routing\\{closure}(Object(Illuminate\\Http\\Request))",
			"36 /opt/bitnami/apps/dreamfactory/htdocs/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(149): Illuminate\\Foundation\\Http\\Middleware\\CheckForMaintenanceMode->handle(Object(Illuminate\\Http\\Request), Object(Closure))",
			"37 /opt/bitnami/apps/dreamfactory/htdocs/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))",
			"38 /opt/bitnami/apps/dreamfactory/htdocs/vendor/barryvdh/laravel-cors/src/HandlePreflight.php(34): Illuminate\\Routing\\Pipeline->Illuminate\\Routing\\{closure}(Object(Illuminate\\Http\\Request))",
			"39 /opt/bitnami/apps/dreamfactory/htdocs/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(149): Barryvdh\\Cors\\HandlePreflight->handle(Object(Illuminate\\Http\\Request), Object(Closure))",
			"40 /opt/bitnami/apps/dreamfactory/htdocs/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))",
			"41 /opt/bitnami/apps/dreamfactory/htdocs/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(102): Illuminate\\Routing\\Pipeline->Illuminate\\Routing\\{closure}(Object(Illuminate\\Http\\Request))",
			"42 /opt/bitnami/apps/dreamfactory/htdocs/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(151): Illuminate\\Pipeline\\Pipeline->then(Object(Closure))",
			"43 /opt/bitnami/apps/dreamfactory/htdocs/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(116): Illuminate\\Foundation\\Http\\Kernel->sendRequestThroughRouter(Object(Illuminate\\Http\\Request))",
			"44 /opt/bitnami/apps/dreamfactory/htdocs/public/index.php(55): Illuminate\\Foundation\\Http\\Kernel->handle(Object(Illuminate\\Http\\Request))",
			"45 {main}"
		],
		"status_code": 400
	}
}

#7

OK I added _ to the exclude list and now I just get an empty “Output” response from test_rest, which is the same as I get when calling the endpoint via Ajax in the browser :frowning: At least it is consistent :slight_smile:


#8

If I edit ./htdocs/vendor/dreamfactory/df-rws/src/Services/RemoteWeb.php and add a print_r around line 421 e.g.

 Log::debug('Outbound HTTP request: ' . $this->action . ': ' . $url);
        Curl::setDecodeToArray(true);
        $result = Curl::request($this->action, $url, $data, $options);
        $resultHeaders = Curl::getLastResponseHeaders();
print_r($result);
exit;

then I can see the response from the Stripe endpoint in test-rest output section, so the code path to call Stripe is working. It’s just getting eaten up somewhere along the way from DF back to the browser. The response is valid JSON.


#9

Thanks @Tim_lloyd! Passing to the engineers to get some help here.


#10

Thanks, I appreciate that. Is there another way I could wrap the Stripe API, perhaps using custom script and curl ?