Transfer Credentials from Web Form to WiFiNINA

Hi There
I try to get the credentials I input on a website (served by my MKR1010 in AP mode) passed trough to WiFi.begin of the WiFiNINA Library. Somehow it doesn't work. If I hardcode the credentials everything works fine...
I have a String that I get from the http request, which is OK (tested via print()) and I convert it here:

char *strToChar(String str) {
int len = str.length() + 1;
char c[len];
str.toCharArray(c, len);
return c;
}

to use it here:

WiFi.begin(strToChar(network), strToChar(password));

any ideas or someone who did this before?

I do do this, but not using a function, but just as a local declaration.

void TryConnect(String ssid, String password) {  // try to connect to this network
  char namebuf[ssid.length()+1];
  ssid.toCharArray(namebuf,ssid.length()+1);
  char passbuf[password.length()+1];
  password.toCharArray(passbuf,password.length()+1); 

  WiFi.begin(namebuf, passbuf);

}

this does work for me.