3

very new to server side programming, so my apologies in advance

I created a google cloud function that calls an API (i figured that part out it works ), the cloud function is invoked via http. for my environment am using Node Js and am using axios to make api call from within my gcloud function

now am stuck on 1) how do I send the data to the gcloud function (I am assuming a POST, and am also assuming it is sent in the body?) at the moment am using Postman to call the gcloud function. I have been experimenting with variations but I get nothing but Internal Server Errors.

2) How to use the parameters I sent to the google cloud function.

am assuming something like below? but am really not sure

exports.helloWorld = async(x,y,req, res) => {

thanks in advance

slair
  • 39
  • 6

1 Answers1

1

Yes, you are correct, POST is one of the most used methods to send data via your calls. You need to look out into some specific topics, such as for the type of data that you will be sending and how it's going to be handled and with CORS (Cross-origin resource sharing ) as well, since it be will crossing origins and this usually causes errors.

Including that, you are almost correct on how to use the parameters. It will look more like this below - this code and other examples are available in the official documentation.

exports.helloWorld = functions.https.onRequest((req, res) => {
  // ...
});

You can continue to use Postman to call the functions, but you can use a cURL as well, to do that. The cURL will be something like this:

curl -X POST "https://YOUR_REGION-YOUR_PROJECT_ID.cloudfunctions.net/FUNCTION_NAME" -H "Content-Type:application/json" --data '{"name":"Keyboard Cat"}'

I would recommend you to take a look at the official documentation Call functions via HTTP requests to get a better understanding of how it works. Besides that, this other post from the Community here, should provide you with a complete example of Cloud Functions with HTTP and Node.js.

Let me know if the information helped you!

gso_gabriel
  • 4,199
  • 1
  • 10
  • 22
  • cool thanks! I will look through the info provided and try it! – slair Jun 05 '20 at 16:44
  • Please, consider upvoting or accepting my answer, in case you think it helped you. – gso_gabriel Jun 12 '20 at 05:52
  • thanks a ton I didn't want to start a whole new post for this item..but I have a gcloud function that writes to firestore and am trying to write some information to the realtime database await firebase.database().ref('users/' + userId).set({ usr_id: usr.id, }); that should just write the newly created user id to the database but the database is unchanged await fireabse().database.ref.set(msgstatus) ; but neither is updating the realtime database what am i missing? – slair Aug 20 '20 at 19:20