Code Monkey home page Code Monkey logo

Comments (8)

restoredtemecula avatar restoredtemecula commented on July 17, 2024 1

I was able to make Firebase work without the cors-anywhere library being required and I have been using it for POST requests as well. It looks like there is helpful cors functionality already baked into firebase functions now, if that helps others with their use case.

I copied the code I am using below in my index.js file.

"use strict";

const functions = require("firebase-functions");
const cors = require("cors")({ origin: true });

const fetch = require("node-fetch");


exports.cors = functions.https.onRequest((req, res) => {
  cors(req, res, () => {
    console.log("Query:", req.query);
    console.log("Body:", req.body);

    let url = req.query.url;

    if (!url) {
      url = req.body.url;
    }

    if (!url) {
      res.status(403).send("URL is empty.");
    }

    console.log("Request:", url);

    // disallow blocked phrases
    if (url.match(blockedPhrases)) {
      res.status(403).send("Phrase in URL is disallowed.");
    }

    fetch(url, {
      method: req.method,
      body:
        req.get("content-type") === "application/json"
          ? JSON.stringify(req.body)
          : req.body,
      headers: {
        "Content-Type": req.get("Content-Type"),
      },
    })
      .then((r) =>
        r.headers.get("content-type") === "application/json"
          ? r.json()
          : r.text()
      )
      .then((body) => res.status(200).send(body));
  });
});

from cors-server.

isoaxe avatar isoaxe commented on July 17, 2024

Hello @camilohr18. Yes this sounds like a problem with the server you are trying to contact. Going by your description, it sounds like you are using the proxy correctly.

Give me the URL of the server you are trying to contact along with with the request method (e.g. GET or POST) and I'll take a look. Also, is there any auth token required in the request?

from cors-server.

camilohr18 avatar camilohr18 commented on July 17, 2024

Hi @isoaxe thanks forn answer. I dont think the problem is the server, because with get works without any problem. The error shows only with POST. Here are the information you need to make test.

This are test keys:
Public Key: 332c12e2ca705bfdaca15fa03bf338e1
Privatekey: 2c9d674e-3547-76c8-0575-befe5343d810

I can make "get" request like this:

import axios from 'axios';

const options = {
  method: 'GET',
  url: 'https://proxy-hidpatd74q-uc.a.run.app/https://scl-sandbox.dev.clover.com/pakms/apikey',
  headers: {
    accept: 'application/json',
    authorization: 'Bearer 2c9d674e-3547-76c8-0575-befe5343d810'
  }
};

axios
  .request(options)
  .then(function (response) {
    console.log(response.data);
  })
  .catch(function (error) {
    console.error(error);
  });

But when I try to make POST request like this:

const options = {
  method: 'POST',
  headers: {
    accept: 'application/json',
    apikey: '332c12e2ca705bfdaca15fa03bf338e1',
    'content-type': 'application/json'
  },
  url: 'https://proxy-hidpatd74q-uc.a.run.app/https://token-sandbox.dev.clover.com/v1/tokens',
  data: {
    card: {
      brand: 'DISCOVER',
      number: '6011361000006668',
      exp_month: '12',
      exp_year: '2024',
      cvv: '123',
      last4: '6668',
      first6: '601136'    
    }
  }
};

axios
  .request(options)
  .then(function (response) {
    console.log(response.data);
  })
  .catch(function (error) {
    console.error(error);
  });

THAT DOES NOT WORK. And I did it before with the heroku server directly from CORS Anywhere. Firebase was showing an error that says "The request has been terminated because it has reached the maximum request timeout. To change this limit, see https://cloud.google.com/run/docs/configuring/request-timeout" and that happened always afer 15s, and to solve it I duplicated the server with same information but extending timeout to 3600s and did not work either. So after a lot of time lost I think maybe its a problem with the code or a problem with the server. If you are able to check the code and confirm its not the code, let me know, because I'm not an expert in nodejs.

by the way, here are the 2 different servers with temout:

https://proxy-hidpatd74q-uc.a.run.app ---> Timeout=60s
https://proxy-1-hidpatd74q-uc.a.run.app ---> Timeout= 3600s

Thank you so much

from cors-server.

isoaxe avatar isoaxe commented on July 17, 2024

I think there might be an issue with hosting a proxy with Firebase and making POST requests.

I'd advise making a proxy with Fly.io and seeing if that works.

from cors-server.

isoaxe avatar isoaxe commented on July 17, 2024

Actually before you do that, give me the URL that you're making the call from (i.e. the web address where your code is being hosted). I'll whitelist it on a test server I have so you can quickly see if hosting with Fly will resolve your issues.

from cors-server.

camilohr18 avatar camilohr18 commented on July 17, 2024

I already try with you fly.io deploy, and work, so I think its something about the deploy on firebase that is not working. I asked directly to someone that had same issue like a year ago in the CORS git and his answer was to deploy in another app host. If you found a solution will be great, and let me know, but I give up on firebase and went to render.com and the deploy work perfectly.

from cors-server.

isoaxe avatar isoaxe commented on July 17, 2024

I only used Firebase for GET requests. I think it's not possible to use a Firebase proxy for any mutative operations. See the first two issues in this repo that relate to this - I had tried extensively using cURL with @restoredtemecula.

I think you should forget about Firebase and use one of the provides that work with POST requests.

from cors-server.

isoaxe avatar isoaxe commented on July 17, 2024

Thanks for the update, that's good to know.

from cors-server.

Related Issues (9)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.