Connecting to postgres online

i have a .net , angular , and postgres webapp online hosted by fly.io . everything works fine,
i converted it to ionic , when i try to connect to the server which is online i am getting an error , " Http faliure response for https:// example-url .fly.dev/user/login : 0 Unknown faliure"

is there something to do with permissions to connect to the internet ,? cause i didnt ask for no permission at all ,

is there something else?

There aren’t any special permissions to connect to the internet. Can you share some code of what you are trying to do? Have you checked DevTools to see if there are any errors?

sorry, i forgot to mention that it’s in production apk app already, development works fine, when im trying to call dot net server hosted in fly.io im getting this error.

בתאריך יום ב׳, 28 באוק׳ 2024, 15:32, מאת Tom Westrick via Ionic Forum ‏<notifications@ionicframework.discoursemail.com>:

Some code would still be helpful. You say development works fine, is this in the browser or on a physical device or emulator?

both give the same result, what should i share?

בתאריך יום ב׳, 28 באוק׳ 2024, 16:01, מאת Tom Westrick via Ionic Forum ‏<notifications@ionicframework.discoursemail.com>:

the server is hosted in fly.io works fine with web app api calls
and i know that on a mobile device there arent any cors so what could be the problem??

here is android manifest

<?xml version="1.0" encoding="utf-8"?>




also the server has an ssl certificate .

‫בתאריך יום ב׳, 28 באוק׳ 2024 ב-16:05 מאת ‪avihu siso‬‏ <‪sisoavihu@gmail.com‬‏>:‬

Some of your info isn’t coming through. You might need to post directly via the Forum and not via email :person_shrugging:

CORS can be an issue in an Ionic/Capacitor app. So, if you run your app via XCode or Android Studio on a device, does it work?

sorry for the trouble.
im posting directly from the forum,
i checked via emulator and via an android device.
im able to reach the first login page , it shows and everything but when i try to reach the backend then im having some trouble .
cause there isnt really a console log to show the errors i created a toastr service that pops up with the error , and the error is http faliure response etc , unkown error .

thank you for your time.

You can access DevTools for Android in Chrome here - chrome://inspect/#devices.

For iOS, see xcode - How do I open developer tools on iOS Simulator? - Stack Overflow

i need to mention that dotnet on fly.io has to be dockerized. is it a problem to reach a dockerized server for ionic??

i know but there isnt devtools on the device itself.

You can access DevTools for the app running on your phone by going to the links I gave on your computer.

and also it does show the error , the error is http faliure response error unknown error 0

Please share a screenshot of the error from DevTools of the Console tab and the Network tab.

also this is my program.cs

using server.Data;
using server.Services;
using Microsoft.EntityFrameworkCore;
using server.Helpers;

var builder = WebApplication.CreateBuilder(args);

builder.WebHost.ConfigureKestrel(serverOptions =>
{

serverOptions.Limits.MaxRequestBodySize = 524288; // 50 MB
// Other Kestrel settings

});

// Add services to the container.
builder.Services.AddControllers();
// builder.Services.AddEndpointsApiExplorer();
// builder.Services.AddSwaggerGen();
builder.Services.AddScoped();

// Add database context
// builder.Services.AddDbContext(options =>
// options.UseNpgsql(builder.Configuration.GetConnectionString(“DefaultConnection”)));
var connString = “”;
if (builder.Environment.IsDevelopment())
connString = builder.Configuration.GetConnectionString(“DefaultConnection”);
else
{
// Original connection URL from environment variable
var connUrl = Environment.GetEnvironmentVariable(“DATABASE_URL”);

// Check if the connection URL is null or empty
if (string.IsNullOrEmpty(connUrl))
{
    Console.WriteLine("Error: Connection URL is missing.");
    return;
}

// Remove 'postgres://' prefix
connUrl = connUrl.Replace("postgres://", string.Empty);
Console.WriteLine($"After removing 'postgres://': {connUrl}");

// Split the connection string to extract user info and host details
var pgUserPassHost = connUrl.Split("@");
if (pgUserPassHost.Length != 2)
{
    Console.WriteLine("Error: Invalid connection string format.");
    return;
}

var pgUserPass = pgUserPassHost[0];
var pgHostPortDb = pgUserPassHost[1];

// Split user info into username and password
var userPassSplit = pgUserPass.Split(":");
if (userPassSplit.Length != 2)
{
    Console.WriteLine("Error: Invalid user and password format.");
    return;
}
var pgUser = userPassSplit[0];
var pgPass = userPassSplit[1];

// Split host and port from the database name
var hostPortDbSplit = pgHostPortDb.Split("/");
if (hostPortDbSplit.Length != 2)
{
    Console.WriteLine("Error: Host and database split error.");
    return;
}
var pgHostPort = hostPortDbSplit[0];
var pgDb = hostPortDbSplit[1];

// Split host and port
var hostPortSplit = pgHostPort.Split(":");
if (hostPortSplit.Length != 2)
{
    Console.WriteLine("Error: Invalid host and port format.");
    return;
}
var pgHost = hostPortSplit[0];
var pgPort = hostPortSplit[1];

// Construct the connection string for Npgsql
connString = $"Server={pgHost};Port={pgPort};User Id={pgUser};Password={pgPass};Database={pgDb};";
Console.WriteLine($"Final connection string: {connString}");

}
builder.Services.AddDbContext(opt =>
{
opt.UseNpgsql(connString);
});
// Add Identity services
builder.Services.AddIdentityServices();

// Add JWT services
builder.Services.AddJwtServices(builder.Configuration);

if (builder.Environment.IsDevelopment())
{
builder.Services.AddCors(options =>
{
options.AddPolicy(“AllowSpecificOrigin”,
builder => builder.WithOrigins(
http://localhost:8100”,
“capacitor://localhost”,
“ionic://localhost”)
.AllowAnyHeader()
.AllowAnyMethod()
.AllowCredentials());
});

}
else
{
builder.Services.AddCors(options =>
{
options.AddPolicy(“AllowSpecificOrigin”,
builder => builder.WithOrigins(
https://bookin.fly.dev”,
“capacitor://localhost”,
“ionic://localhost”)
.AllowAnyHeader()
.AllowAnyMethod()
.AllowCredentials());
});
}
var app = builder.Build();
app.Lifetime.ApplicationStarted.Register(() =>
{
var addresses = app.Urls;
foreach (var address in addresses)
{
Console.WriteLine($“Kestrel is listening on {address}”);
}
});
// Initialize the database
await DatabaseInitializer.InitializeAsync(app.Services);

app.UseCors(“AllowSpecificOrigin”);

app.UseAuthentication();
app.UseAuthorization();

app.UseDefaultFiles();
app.UseStaticFiles();

app.MapControllers();
app.MapFallbackToController(“Index”, “FallBack”);

app.Run();

Please use proper code blocks so we can easily read your code.

From the error you posted, IT IS a CORS error.

The default origins for a Capacitor app are as follows:

  • iOS: capacitor://localhost
  • Android (prior to Capacitor 6): http://localhost
  • Android (default in Capacitor 6): https://localhost

It looks like you have ionic://localhost for Android? If you are using Capacitor’s defaults, that is incorrect.

i just tried everything this was the final try,
which cors should i use since the web url isnt relevant??
i am using android only ,
the screen shot was from the development mode on my pc , and the localhost:8100 was included inside the cors for both development and production

Use what I said:

The URL is relevant as your app is running in a WebView on Android under the URL’s mentioned above (assuming you haven’t overriden them in your capacitor.config.ts file.

For CORS, the domain and port have to match, so localhost:8100 is different than localhost.

You can find additional info about CORS here.

here is my capacitor.config.ts

import type { CapacitorConfig } from '@capacitor/cli';

const config: CapacitorConfig = {
  appId: 'io.ionic.starter',
  appName: 'bookitV2',
  webDir: 'www',
  server: {
    allowNavigation: [
      'bookin.fly.dev', // Your production API domain
      'localhost',       // To allow local dev testing
      'capacitor://localhost' // For mobile access
    ],
    cleartext: true, // For Android HTTP traffic if needed
  },
};

export default config;

do i change anything in the program.cs ?? please guide me

Add the two Android domains that I mentioned to builder.Services.AddCors -> WithOrigins.