最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

reactjs - error when i want to call others api integrate via proxy.ts - Stack Overflow

programmeradmin3浏览0评论

I have a website built on next.js but when i test on localhost:3000/login`. when input the id and password I am forward the proxy using this

http://localhost:3000/api/proxy/?service=auth&endpoint=api/v1/auth/token

my proxy will be directed to others API call to our third-party API

i can login. and i can get the response from others idm integration

but when I am deploying on my server running on ec2. using www.xxxx/login and call http://localhost:3000/api/proxy/?service=auth&endpoint=api/v1/auth/token

my proxy cannot redirect and cannot call our third-party API idm

here is my proxy.ts code

import { NextApiRequest, NextApiResponse } from 'next';
import axios from 'axios';

export default async function handler(req: NextApiRequest, res: NextApiResponse) {

  const { endpoint, service } = req.query;

    const serviceBaseURLs: Record<string, string> = {
    auth: process.env.XYXS_API_URL || '',
    otherServices: process.env.ABCD_API_URL || 'http://abcd:9020',
  };
  const baseURL = serviceBaseURLs[service as string];

    const clientId = process.env.PUBLIC_CLIENT_ID || 'ppoc';
  const clientSecret = process.env.PUBLIC_CLIENT_SECRET || 'ppocjbndel';
  const authorization = 'Basic ' + Buffer.from(`${clientId}:${clientSecret}`).toString('base64');

    try {
    const response = await axios({
      method: req.method,
      url: `${baseURL}/${endpoint}`,
      headers: {
        Authorization: authorization,
        'Content-Type': req.headers['content-type'] || 'application/json',
      },
      data: req.body,
    });

    res.status(response.status).json(response.data);
  } catch (error: any) {
    res.status(error.response?.status || 500).json({ error: error.message });
  }
}

How do I fix the issue on my ec2 process? I expected that my proxy call on ec2 can be redirected to my third-party API

发布评论

评论列表(0)

  1. 暂无评论