What's new

Help Passing .crt and .key file in API POST request sa NEXTJS

Kosei arima-Kun

Leecher
Joined
Jul 21, 2023
Posts
11
Reaction
3
Points
4
Hello mga paps, meron ba dito ang may experience sa pag API request sa API URL na need mag pasa ng client certificate(filename.crt at filename.key) sa NextJS. Sa Postman nagawa ko sya ang nakakuha ng expected ko na response, nag post request ako sa mismong api url at nag add ng client certificate sa settings specifically sa API URL, at naka enable ang 'Follow original HTTP Method'. Pano nyo po nagawa sa (route.ts or route.js) sa api end point nyo po sa NEXTJS. May sample ako ng code ko nag pass ako ng httpsAgent tapos empty response tapos method not allow, feeling ko nag change si axios ng method after redirect, tapos isa naka kino-mment sa part ng httpsAgent tapos may HTML reponse na need ng ssl certificate. Thank you in Advance sa help:).



Screenshot (39).png


Screenshot (40).png


Screenshot (41).png


Screenshot (42).png
 

Attachments

Last edited:
Hi there! It seems like you're having trouble making an API request in NextJS while passing a client certificate. I can help you with that!

To make an API request in NextJS while passing a client certificate, you can use the https module in Node.js. Here's an example of how you can do it in NextJS:

JavaScript:
import https from 'https';
import fs from 'fs';
import axios from 'axios';

const certFile = fs.readFileSync('path/to/filename.crt');
const keyFile = fs.readFileSync('path/to/filename.key');

const agent = new https.Agent({
  cert: certFile,
  key: keyFile
});

const fetchData = async () => {
  try {
    const response = await axios.post('https://api-url.com', { data }, { httpsAgent: agent });
    console.log(response.data);
  } catch (error) {
    console.error(error);
  }
};

In this example, we're first reading the certificate and key files using the fs module. Then, we create an instance of the https.Agent class and pass the certificate and key files as options. Finally, we make the API request using axios and pass the httpsAgent option with the agent we created.

Make sure to replace 'path/to/filename.crt' and 'path/to/filename.key' with the actual paths to your certificate and key files.

I hope this helps! Let me know if you have any further questions.
 
Hello! I understand that you are looking for help regarding passing a client certificate in an API request in NextJS. It seems that you have already done this successfully in Postman, but you are facing issues when trying to implement it in your NextJS project.

To pass a client certificate in an API request in NextJS, you can use the https module available in Node.js. Here's an example of how you can achieve this:

JavaScript:
import https from 'https';
import fs from 'fs';
import axios from 'axios';

// Load the client certificate and key
const cert = fs.readFileSync('filename.crt');
const key = fs.readFileSync('filename.key');

// Create an https agent with the client certificate and key
const agent = new https.Agent({
  cert: cert,
  key: key,
});

// Make the API request using Axios with the https agent
axios
  .post('https://api.example.com/endpoint', { data }, { httpsAgent: agent })
  .then((response) => {
    // Handle the response
  })
  .catch((error) => {
    // Handle the error
  });

In this example, we first load the client certificate and key using the fs module. Then, we create an HTTPS agent with the client certificate and key using the https.Agent class. Finally, we make the API request using Axios and pass the httpsAgent option with the agent we created.

Make sure to replace filename.crt and filename.key with the actual paths to your client certificate and key files.

Please note that this example assumes that you have the certificate and key files stored locally. If you need to pass the certificate and key as strings, you can use Buffer.from to convert them.

I hope this helps! Let me know if you have any further questions.
 

Similar threads

Back
Top