1

I'm trying to send rdata in request but I can not get rdata because /callback URL is changed So I tried to do it like this :

client

 useEffect(() => {
    const apiCall = async () => {
      const response = await axios.get(`http://localhost:5000/callback:id`);
      console.log(response);
    };
    apiCall();
  }, []);

server

app.get("/callback", cors(), (req, res) => {

  if (req.query.code !== null && req.query.code !== undefined) {
    var token_url = "https://www.ssoservice.kr/oauth2.0/token";
    var options = {
      url: token_url,
      method: "POST",
      form: {
        grant_type: "authorization_code",
        client_id: client_id,
        client_secret: client_secret,
        redirect_uri: redirectURI,
        code: req.query.code,
        state: req.query.state,
      },
    };

    request(options, function (error1, response1, body1) {
      if (!error1 && response1.statusCode == 200) {
        var tdata = JSON.parse(body1);
        var options2 = {
          url: "https://www.ssoservice/oauth2.0/resource",
          headers: {
            "Content-Type": "application/json",
            Authorization: "Bearer " + tdata.access_token,
          },
        };
        request(options2, function (error2, response2, body2) {
          if (!error2 && response2.statusCode == 200) {
            var rdata = JSON.parse(body2);
            res.writeHead(200, { "Content-Type": "text/json;charset=utf-8" });
            res.end(rdata);

            console.log(rdata); 
            ////////////////this data////////////
          } else {
            res.status(response2.statusCode).end();
            console.log("error2 = " + response2.statusCode);
          }
        });
      } else {
        res.status(response1.statusCode).end();
        console.log("error1 = " + response1.statusCode);
      }
    });
  }
});
however, I can not get the rdata
Flagmans
  • 159
  • 1
  • 2
  • 16
Somi
  • 141
  • 6
  • 1
    use `res.json(rdata);` instead of `res.end(rdata);` . Also your url does not match. There is an extra `:id` in your client request url. – Ricky Mo Sep 29 '21 at 05:19
  • Thank you for answering, How can I get the URL query in client from server URL ?? – Somi Sep 29 '21 at 05:21
  • 1
    [https://stackoverflow.com/questions/6912584/how-to-get-get-query-string-variables-in-express-js-on-node-js](https://stackoverflow.com/questions/6912584/how-to-get-get-query-string-variables-in-express-js-on-node-js) – Ricky Mo Sep 29 '21 at 05:22
  • Sir, I can get the query in node.js but I want to know how to send the query in client side Would you tell me how to if u don't mind? – Somi Sep 29 '21 at 05:29
  • 1
    #1 Did you fix your issue? #2 If it is query parameter, just append to the url `axios.get(`http://localhost:5000/callback?id=123456`)` – JRichardsz Sep 29 '21 at 05:32

0 Answers0