It seems like you're encountering an SSL/TLS certificate validation issue when trying to call an external web API from D365FO X++. This error typically occurs when the client application doesn't trust the SSL certificate presented by the server.
To resolve this, you can try the following steps:
1. **Ensure Trustworthy Certificate**: Make sure that the SSL certificate presented by the server is issued by a trusted Certificate Authority (CA) and is not expired or revoked.
2. **Update Security Protocol**: You've already set the `SecurityProtocol` to `Tls12`, which is good practice. However, you might also need to set `System.Net.ServicePointManager::ServerCertificateValidationCallback` to a custom validation method if you're using a self-signed certificate or have specific certificate validation requirements.
3. **Check Proxy Settings**: If your network uses a proxy, ensure that the proxy settings are correctly configured to allow the communication between your application and the external API.
4. **Verify URL**: Double-check the URL you are using to make sure it's correct and accessible from your network environment.
5. **Inspect Headers and Payload**: Ensure that the headers and payload you're sending are correctly formatted and match the API's requirements.
6. **Debugging**: Try to capture more detailed information about the error by logging on
video editor or debugging the SSL handshake process to identify the exact cause of the issue.
Here's a modified version of your code incorporating some of these suggestions:
```x++
try
{
// Send out the payload
using (System.IO.Stream dataStream = request.GetRequestStream())
{
dataStream.Write(byteArrayPayload, 0, byteArrayPayload.Length);
}
// Get the response
using (System.Net.HttpWebResponse response = request.GetResponse() as System.Net.HttpWebResponse)
{
stream = response.GetResponseStream();
System.IO.StreamReader reader = new System.IO.StreamReader(stream);
str responseBody = reader.ReadToEnd();
}
}
catch (System.Net.WebException ex)
{
// Log or handle the exception
str errorMessage = ex.ToString();
throw error(errorMessage);
}
```
This code block catches the `WebException` specifically, which includes detailed information about the underlying SSL/TLS error. You can log or handle this exception to get more insights into the problem.
Remember to replace `error(errorMessage)` with the appropriate error handling mechanism in your application.
If the issue persists after trying these steps, you might need to consult with your network administrator or the provider of the external API for further assistance.