Hello everyone. I’m having an issue while using the CapacitorHTTP in my project. Im trying to get items from a database via a url. For Android it works fine, I am able to retrieve the items but on iOS, I get this error in the log:
Here is what my get method looks like:
async getLocations(): Promise<any> {
const options = {
url: 'https://example.com/my/api',
headers: {
'Content-Type': 'application/json',
}
};
return await CapacitorHttp.get(options);
}
Heres what Android looks like:
I followed this guide: Capacitor Http Plugin API | Capacitor Documentation
I have found this issue in the GitHub repo that is similar to my issue:
opened 02:11PM - 20 Jan 23 UTC
closed 02:21PM - 20 Jan 23 UTC
platform: android
platform: ios
## Bug Report
### Capacitor Version
```
💊 Capacitor Doctor 💊
Latest … Dependencies:
@capacitor/cli: 4.6.2
@capacitor/core: 4.6.2
@capacitor/android: 4.6.2
@capacitor/ios: 4.6.2
Installed Dependencies:
@capacitor/cli: 4.6.1
@capacitor/android: 4.6.1
@capacitor/core: 4.6.1
@capacitor/ios: 4.6.1
[success] iOS looking great! 👌
```
### Platform(s)
iOS (but not tested on Android yet)
### Current Behavior
```
try {
console.log("Sending request");
const resp = await CapacitorHttp.request({
url: "http://localhost:3002",
readTimeout: 1000,
connectTimeout: 1000,
});
console.log("Response", resp);
} catch (e: any) {
console.error("ERROR", e);
}
```
Assuming that `localhost:3002` is not actually reachable (can be any non-reachable server) what I see in the iOS simulator logs now is:
```
⚡️ [log] - Sending request
⚡️ To Native -> CapacitorHttp request 43313132
2023-01-20 15:07:36.143108+0100 SilverBullet[38280:9724338] [connection] nw_socket_handle_socket_event [C1.1.1:2] Socket SO_ERROR [61: Connection refused]
2023-01-20 15:07:36.144018+0100 SilverBullet[38280:9724338] [connection] nw_socket_handle_socket_event [C1.1.2:2] Socket SO_ERROR [61: Connection refused]
2023-01-20 15:07:36.144812+0100 SilverBullet[38280:9724338] Connection 1: received failure notification
2023-01-20 15:07:36.145062+0100 SilverBullet[38280:9724338] Connection 1: failed to connect 1:61, reason -1
2023-01-20 15:07:36.145113+0100 SilverBullet[38280:9724338] Connection 1: encountered error(1:61)
2023-01-20 15:07:36.145774+0100 SilverBullet[38280:9724338] Task <2D5EEC72-607D-4827-A435-1CCCE5755803>.<1> HTTP load failed, 0/0 bytes (error code: -1004 [1:61])
2023-01-20 15:07:36.148997+0100 SilverBullet[38280:9724342] Task <2D5EEC72-607D-4827-A435-1CCCE5755803>.<1> finished with error [-1004] Error Domain=NSURLErrorDomain Code=-1004 "Could not connect to the server." UserInfo={_kCFStreamErrorCodeKey=61, NSUnderlyingError=0x600003e09f80 {Error Domain=kCFErrorDomainCFNetwork Code=-1004 "(null)" UserInfo={_NSURLErrorNWPathKey=satisfied (Path is satisfied), interface: en0[802.11], _kCFStreamErrorCodeKey=61, _kCFStreamErrorDomainKey=1}}, _NSURLErrorFailingURLSessionTaskErrorKey=LocalDataTask <2D5EEC72-607D-4827-A435-1CCCE5755803>.<1>, _NSURLErrorRelatedURLSessionTaskErrorKey=(
"LocalDataTask <2D5EEC72-607D-4827-A435-1CCCE5755803>.<1>"
), NSLocalizedDescription=Could not connect to the server., NSErrorFailingURLStringKey=http://localhost:3002/, NSErrorFailingURLKey=http://localhost:3002/, _kCFStreamErrorDomainKey=1}
2023-01-20 15:07:36.149091+0100 SilverBullet[38280:9724342] [NSURLSession sharedSession] may not be invalidated
```
So clearly Capacitor itself is detecting that it cannot reach this URL, however it never makes it back into "JS land". Neither the catch, nor the console.log("Response", ...) line ever gets called.
### Expected Behavior
Either throw an exception or return a response with some response error code, either would work (exception would make more sense, probably).
### Code Reproduction
See the code listed above
### Other Technical Details
<!--
Please provide the following information with your request and any other relevant technical details (versions of IDEs, local environment info, plugin information or links, etc).
-->
`npm --version` output:
8.12.1
`node --version` output:
v18.4.0
`pod --version` output (iOS issues only):
1.11.3
@jcesarmobile stated that it was fixed in 4.6.2 but im on 4.6.3 and im still seeing the issue.
Can anyone please help me get this working in iOS?
Anyone have any ideas or can point me in the right direction?
Have you tried to use .request instead of get?
return await CapacitorHttp.request(options);
Yes, still shows the same error.
So I just decided to use cordova-plugin-advanced-http instead. In conclusion, CapacitorHttp does NOT work properly, not sure whats going on under the hood but switching to the cordova plugin worked in one shot. Thanks for you help @Hills90210 !
1 Like
add .catch and print error messages, maybe there’s a more precise explanation