我对React Native和应用程序开发还很陌生。我对向服务(本例中为瑞典BankID)发出POST请求有疑问,该请求需要客户端证书和客户端密钥(.pem / .key文件)才能请求API。
在PHP中,我通过卷曲并使用CURLOPT_SSLCERT
和CURLOPT_SSLKEY
并仅从运行PHP的服务器直接读取证书文件来完成了多次。
但是如何在React Native中将fetch()
与客户端证书和客户端密钥一起使用? (我正在使用Expo)
我已经尝试过了,但是看到的错误如下:
const options = {
cert: require("../assets/certs/cert.pem"),
key: require("../assets/certs/key.key"),
passphrase: "qwerty123",
rejectUnauthorized: false,
keepAlive: false,
};
const sslConfiguredAgent = new https.Agent(options);
try {
let response = await fetch("https://appapi2.test.bankid.com/rp/v5/auth", {
method: "POST",
headers: {
Accept: "application/json",
"Content-Type": "application/json",
},
body: JSON.stringify({
personalNumber: "199010240000",
endUserIp: "127.0.0.1",
}),
agent: sslConfiguredAgent,
timeoutInterval: 5000,
});
response = await response.json();
console.log(response);
} catch (err) {
console.log("Error fetching data-----------", err);
}