Fetch function works in postman, but not in react-native, while trying to send an SMS with twilio API
I am trying to make an app that sends an sms when a button is pressed. In my onPress, i use this function.
SendMessage = () => {
try {
fetch("https://api.twilio.com/2010-04-01/Accounts/[AccountSID]/Messages.json", {
body: "To=[toPhoneNumber]&From=[fromPhoneNumber]&Body=[message]=",
headers: {
Authorization: "Basic [Auth]",
"Cache-Control": "no-cache",
"Content-Type": "application/x-www-form-urlencoded"
},
method: "POST"
})
}
catch (err) {
console.log("Error fetching data-----------", err);
}
}
When i press the button nothing happens. please bear over with me because i am new to react native.
Related
I have Android Client application which runs and E-comm app and my backend server handles the data with MongoDB now I want to use push Notifications on my android app using FCM maybe using REST API which will be triggered from backend and i am not able to do so .
Please guide on how to use FCM from backend to android application.
I tried using FCM documentation and the following code:
var request = require('request');
var options = { method': 'POST', 'url': 'https://fcm.googleapis.com/fcm/send', 'headers': { 'Content-Type': 'application/json' },
body: JSON.stringify({ "to": "YOUR_FCM_TOKEN_WILL_BE_HERE", "collapse_key": "type_a",
"data": { "body": "Sending Notification Body From Data",
"title": "Notification Title from Data" }})};
request(options, function (error, response) { if (error) throw new Error(error); console.log(response.body); });
I am trying to set up cloud recording in Agora.io video call.According to agora docs first step is to call acquire API.
Initially I had issue with unescaped character is URL using axios NodeJS so I used encodeURI to bypass that error.My requests config is as follows
{
"url":"https://api.agora.io%E2%80%8B/v1%E2%80%8B/apps%E2%80%8B/xxxxxxx_APPID_xxxx%E2%80%8B/cloud_recording%E2%80%8B/acquire",
"method":"post",
"data":"{\"cname\":\"5f30xxxx-xx33-xxa9-adxx-xxxxxxxx\",\"uid\":\"75\",\"clientRequest\":{\"resourceExpiredHour\":24}}",
"headers":{
"Accept":"application/json, text/plain, */*",
"Content-Type":"application/json;charset=utf-8",
"Authorization":"Basic xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"User-Agent":"axios/0.19.2",
"Content-Length":102
},
"transformRequest":[
null
],
"transformResponse":[
null
],
"timeout":0,
"xsrfCookieName":"XSRF-TOKEN",
"xsrfHeaderName":"X-XSRF-TOKEN",
"maxContentLength":-1
}
I get this response
Error: Request failed with status code 400
I have enabled cloud recording in agora console still the same error.
I would recommend taking a look at the Agora Postman Collection, which helps provide properly formatted requests.
In your request you are missing the
For example your request should look like this:
var axios = require('axios');
var data = JSON.stringify({"cname":"demo","uid":"527841","clientRequest":{ "resourceExpiredHour": 24}});
var config = {
method: 'post',
url: 'https://api.agora.io/v1/apps/<xxxx_APPID_xxxx>/cloud_recording/acquire',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Basic xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
},
data : data
};
axios(config)
.then(function (response) {
console.log(JSON.stringify(response.data));
})
.catch(function (error) {
console.log(error);
});
Here is a guide I've written for getting started with the Postman Collection for Agora's Cloud Recording.
I set up my express app to listen for Paypal webhooks of my sandbox app. Then I try to verify the integrity of the data via the verify-webhook-signature API endpoint. I use the request module for that. But all I get is a 415 Unsupported Media Type status code and an empty body. This is my code:
app.post('/webhooks', function (req, res) {
if (req.body.event_type == 'PAYMENT.CAPTURE.COMPLETED') {
headers = {
'Accept' : 'application/json',
'Content-Type' : 'application/json',
'Authorization' : 'Bearer <AUTH_TOKEN>'
}
// Get the data for the API call of the webhook request
data = {
'transmission_id': req.headers['paypal-transmission-id'],
'transmission_time': req.headers['paypal-transmission-time'],
'cert_url': req.headers['paypal-cert-url'],
'auth_algo': req.headers['paypal-auth-algo'],
'transmission_sig': req.headers['paypal-transmission-sig'],
'webhook_id': '41G05244UL253035G',
'webhook_event' : req.body
}
request.post({
url: 'https://api.sandbox.paypal.com/v1/notifications/verify-webhook-signature',
headers: headers,
form: data,
}, (error, response, body) => {
if (error) {
console.error(error)
return
}
console.log(response.statusCode);
});
}
res.sendStatus(200);
});
What is wrong with this data?
Edit: Changing 'form: data' to 'body: JSON.stringify(data)' did it.
Why are you sending back a form post? Don't post a form. Send back raw data.
I'm currently developing my own slack app using NodeJS.
My program calls an external API. Sometimes it is unable to respond within 3000 msec and Slack displays a timeout error. How to handle this error in NodeJS?
The Slack-API provides a response_url to send a request. What is the best solution here?
This is my code. It works for me but it's not the best solution.
if (time_result < 2500) {
return res.ok(foo)
} else {
request({
url: response_url,
headers: {
'content-type': 'application/json',
},
body: foo,
method: 'POST',
json: true
},
function(error, response, body) {
if (error) {
sails.log(error);
} else {
sails.log(body)
}
})
return res.json({
text: 'message is comming :)'
});
}
I programmed a Node.JS Server with FCM and POST Protocol and my Client app is made with Swift code.
In Terminal, It prints success code like this
id=0:1476620858011270%00f7ba1cf9fd7ecd
But In my Client App, there is no notification. So I tried with the notification tab in my Firebase Console, and it worked very well.
This is my header in my server file
var headers = {
'Content-Type': 'application/json',
'Authorization': 'key=<Server Key>',
'project_id': '<Project ID>'
}
and This is my Request Code
request({
headers: headers,
url: "https://fcm.googleapis.com/fcm/send",
method: "POST",
form: {
"notification" :{
"title": "titie",
"body": "body"
},
"content-available": true,
"priority": "high",
"to": "<Firebase Token>"
}
}, function(error, response, body) {
console.log(body);
});