Reputation: 133
I am trying to call a 3rd Party API when a Client goes to a specific route using Swift and Vapor 3; however, I am met with errors despite being able to make the call.
I have taken steps to ensure the error is caused by the get request to the API. Currently, I do nothing with the request other than print the response. Removing the request stops the error from happening.
// Route the user to HomePage
router.get { req -> Future<View> in
let token: String = "THATS_MY_TOKEN"
let client = try req.client()
let bcmsrequest = try client.get("https://api.buttercms.com/v2/posts/?page=1&page_size=10&auth_token=" + token)
print(bcmsrequest)
print("This line prints.")
// Removing the above get request stops the error below from happening
return try req.view().render("welcome")
}
// Here is the stdout printed by the server process:
Server starting on http://localhost:8080
NIO.EventLoopFuture<Vapor.Response>
This line prints.
2019-07-27 11:49:12.249196-0400 Run[6348:121267] [] nw_endpoint_get_type called with null endpoint
2019-07-27 11:49:12.249420-0400 Run[6348:121267] [] nw_endpoint_get_type called with null endpoint, dumping backtrace:
[x86_64] libnetcore-1872
0 libnetwork.dylib 0x00007fff6d188fc8 __nw_create_backtrace_string + 120
1 libnetwork.dylib 0x00007fff6ce12af4 nw_endpoint_get_type + 180
2 libboringssl.dylib 0x00007fff6b6e3af2 nw_protocol_boringssl_get_subject_name + 178
3 libboringssl.dylib 0x00007fff6b6e6997 nw_protocol_boringssl_connected + 916
4 libnetwork.dylib 0x00007fff6ce5d145 nw_socket_handle_socket_event + 1733
5 libdispatch.dylib 0x00000001017fd82f _dispatch_client_callout + 8
6 libdispatch.dylib 0x0000000101800689 _dispatch_continuation_pop + 585
7 libdispatch.dylib 0x0000000101816608 _dispatch_source_invoke + 2135
8 libdispatch.dylib 0x0000000101807665 _dispatch_workloop_invoke + 3477
9 libdispatch.dylib 0x0000000101813025 _dispatch_workloop_worker_thread + 676
10 libsystem_pthread.dylib 0x000000010188f343 _pthread_wqthread.cold.1 + 125
11 libsystem_pthread.dylib 0x0000000101889196 _pthread_wqthread + 203
12 libsystem_pthread.dylib 0x0000000101889057 start_wqthread + 15
I can see that a Future Object is being printed out, I would expect to see response content (a JSON string) - but I believe that the response has no content and is actually failing to make a request at all.
Upvotes: 0
Views: 390
Reputation: 5565
I can see that a Future Object is being printed out, I would expect to see response content
So this is the crux of the problem. Because Vapor is asynchronous, you're printing the Future<Response>
as soon as you send the request, meaning it hasn't returned yet. If you change it to:
router.get { req -> Future<View> in
let token: String = "THATS_MY_TOKEN"
let client = try req.client()
let bcmsrequest = try client.get("https://api.buttercms.com/v2/posts/?page=1&page_size=10&auth_token=" + token)
return bcmsrequest.flatMap { response in
print(response)
return try req.view().render("welcome")
}
}
You'll see the full response body and see what errors you're getting (if any)
Upvotes: 1