Firebase functions returning 400

I am facing a very strange issue. I am an instructor using Firebase functions as backend of my class application and two of my students are having the same issue that I don't:

My frontend makes a post call to the firebase functions, which access IBM WAtson and give me the answer.

The point is that it works fine for me and when I share my endpoint to them, their codes work, when they share their endpoints to me, my front fails always with 400. I can't explain what is happening. All our codes returns well when we get into the api.

Here is my working url: https://us-central1-chatbot-em-react.cloudfunctions.net/conversa'

Here is the url from one of my students, with the console log:

Request URL: https://us-central1-chatbot-react.cloudfunctions.net/conversa
Request Method: POST
Status Code: 400 
Remote Address: 172.217.11.46:443
Referrer Policy: no-referrer-when-downgrade
access-control-allow-origin: http://localhost:3000
alt-svc: quic=":443"; ma=2592000; v="44,43,39,35"
cache-control: private
content-encoding: gzip
content-length: 159
content-type: application/json; charset=utf-8
date: Wed, 10 Oct 2018 20:51:19 GMT
etag: W/"a8-3BNzCKPhycJhIXE/IBTFVDjBiN0"
function-execution-id: 0fye621c7drg
server: Google Frontend
status: 400
vary: Origin
x-cloud-trace-context: c7353ce928ae47283e9a248dc2cf2147
x-powered-by: Express
Provisional headers are shown
Accept: application/json, text/plain, */*
Content-Type: application/json;charset=UTF-8
Origin: http://localhost:3000
Referer: http://localhost:3000/
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36
{input: "reginaldo", context: {conversation_id: "6a012cbd-d0af-44b3-982d-7b2d6c81b4e9",…}}
context: {conversation_id: "6a012cbd-d0af-44b3-982d-7b2d6c81b4e9",…}
input: "reginaldo"

Are we missing a permission or so? Here is the code we are using:

const functions = require('firebase-functions');
const watson = require('watson-developer-cloud/assistant/v1')
require('dotenv').config()

const cors = require('cors')({ origin: true })

const chatbot = new watson({
    username: process.env.USERNAME,
    password: process.env.PASSW0RD,
    version: process.env.VERSION,
});

const workspace_id = process.env.WORKSPACE_ID;

exports.conversa = functions.https.onRequest((req, res) => {
    cors(req, res, () => {
        let payload = {
            workspace_id,
            context: req.body.context || {},
            input: req.body.input || {}
        };

        chatbot.message(payload, (err, data) => {
            if (err) {
                return res.status(err.code || 500).json(err);
            }

            return res.json(trataResposta(payload, data));
        })
    })
})

const trataResposta = (payload, resposta) => {
    console.log('watson disse: ', resposta.output.text[0]);
    return resposta;
}

We appreciate any given help.

Upvotes: 1

Views: 559

Answers (1)

Martin Zeitler
Martin Zeitler

Reputation: 76569

think it should work without cors ...req.method POST is being supported:

exports.conversa = functions.https.onRequest((req, res) => {
    let payload = {
        workspace_id,
        context: req.body.context || {},
        input: req.body.input || {}
    };
    chatbot.message(payload, (err, data) => {
        if (err) {
            return res.status(err.code || 500).json(err);
        }
        return res.json(trataResposta(payload, data));
    });
});

see Cross-Origin Resource Sharing (CORS).

Upvotes: 1

Related Questions