I'm trying to create a device group as described in the documentation (https://firebase.google.com/docs/cloud-messaging/js/device-group) from a NodeJS backend but I can't make it, I'm always facing an 400 error
Anyone has an idea what I'm doing wrong?
const httpRequest = require('request');
const options = {
url: 'https://fcm.googleapis.com/fcm/notification',
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
'Authorization': 'key=AAA...vR',
'project_id': '76...8'
},
body: JSON.stringify({
operation: 'create',
notification_key_name: 'my-unique-key-name',
registration_ids: ['token1', 'token2']
})
};
httpRequest(options, (error, response, body) => {
if (!error && response.statusCode === 200) {
resolve(Converter.parseJSON(body));
} else {
reject(error);
}
});
Thx in advance for any hints or help!
Turns out that I'm dumb. I printed out the all response:
console.log(response);
and discovered at the very end the reason why it didn't work:
body: '{"error":"notification_key already exists"}' }
so I just tried with another notification_key_name and it worked like a charm
Related
https://sendgrid.api-docs.io/v3.0/contacts/search-contacts
I'm attempting to search for a contact as shown in SendGrids docs above. In the body section below I'd like to change the hard coded "andrew#gmail.com" to be a variable. Such as email = req.user.email; What is the correct way to do that? Just setting the variable and dropping in 'email' does not work.
var request = require("request");
var options = { method: 'POST',
url: 'https://api.sendgrid.com/v3/marketing/contacts/search',
headers:
{ 'content-type': 'application/json',
authorization: 'Bearer SG.key' },
body: { query: 'email LIKE \'andrew#gmail.com\' AND CONTAINS(list_ids, \'6bcc2d0c-ea17-41ba-a4a1-962badsasdas1\')' },
json: true };
request(options, function (error, response, body) {
if (error) throw new Error(error);
console.log(body);
});
Twilio SendGrid developer evangelist here.
Try using string interpolation using back ticks (which, as an added bonus, means you don't have to escape your single quotes), like below:
const email = req.user.email;
const body = `email LIKE '${email}' AND CONTAINS(list_ids, '6bcc2d0c-ea17-41ba-a4a1-962badsasdas1')`;
const options = {
method: 'POST',
url: 'https://api.sendgrid.com/v3/marketing/contacts/search',
headers: {
'content-type': 'application/json',
authorization: 'Bearer SG.key'
},
body: { query: query },
json: true
};
request(options, function (error, response, body) {
if (error) throw new Error(error);
console.log(body);
});
Trying to upload Listing image using ETSY Listing Image API
I am fetching "image_id" & "image" from front-end part.
var request = require("request");
var options = { method: 'POST',
url: 'https://openapi.etsy.com/v2/listings/724108136/images',
qs: { oauth_consumer_key: '*********************',
oauth_token: '****************',
oauth_signature_method: 'HMAC-SHA1',
oauth_timestamp: '**********',
oauth_nonce: '*************',
oauth_version: '1.0',
oauth_signature: '*********************'
},
headers:{
'Content-Type': 'multipart/form-data'
},
encoding: null,
responseType: 'buffer',
data:{
image_id:req.body.image_id,
image:req.files.image
}
};
request(options, function (error, response, body) {
if (error) throw new Error(error);
console.log(body) // Image file or image_id string must be included
});
I think that there is a few things wrong with this.
It looks like you're passing the OAuth info via in the querystring and not in the headers.
You might want to bump up your request version. I have confirmed this is working with "request": "^2.88.2",
`
const request = require("request");
let options = {
'method': 'POST',
'url': 'https://openapi.etsy.com/v2/listings/724108136/images',
'headers': {
'Authorization': 'OAuth oauth_consumer_key="********",oauth_token="***********",oauth_signature_method="HMAC-SHA1",oauth_timestamp="1585021862",oauth_nonce="*******",oauth_version="1.0",oauth_signature="********"',
'Content-Type': 'multipart/form-data'
},
formData: {
'image': {
'value': fs.createReadStream('./temp/abc123.jpg'),
'options': {
'filename': 'my-shirt.jpg',
'contentType': null
}
}
}
};
request(options, function (error, response, body) {
if (error) throw new Error(error);
console.log(body)
});
`
I want to send data to the java back end which is run on tomcat server.this is what i have tried so far.i have already installed request module.get method is working properly.
Router.post('/', function(req, res) {
request({
uri: "http://localhost:8080/HIS_API/rest/UserService/registerUser",
method: "POST",
form: {
roleId:2,
employeeId:26,
userName:"testing",
password:"123"
}
}, function(error, response, body) {
console.log(body);
});
});
You have to use JSON.stringify to send data like in this format. before that write console.log(error). and check what's the error you are getting.
request({
url: url, //URL to hit
method: 'post',
headers: { "Authorization": req.headers.authorization},//if required
timeout: 60 * 1000,
body: JSON.stringify(body)
}, function (error, result, body) {
if (error) {
console.log(error);
} else if (result.statusCode === 500) {
console.log('error');
} else {
console.log(body);
}
});
I am creating a project using Node.js. I want to call my requests in parallel. For achieving this, I have installed the async module. Here is my code:
var requests = [{
url:url,
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + req.cookies.apitoken
},
json: finalArr,
}];
async.map(requests, function(obj, callback) {
// Iterator function
request(obj, function(error, response, body) {
if (!error && response.statusCode == 200) {
// Transform data here or pass it on
var body = JSON.parse(body);
callback(null, body);
}
else {
var body = JSON.stringify(body);
console.log(body)
callback(error || response.statusCode);
}
});
})
I got undefined every time in console.log(body). When I am using GET requests using this module for other requests then everything works fine.
It looks like you're using the request module, but didn't tag it as such.
If I'm right, then your options object is incorrect, from the documentation there's not a data key that's respected. Instead, you should use body or formData or json depending on what kind of data you're pushing up. From your headers, I'd go with json.
var requests = [{
url:url,
method: 'POST',
headers: { // Content-Type header automatically set if json is true
'Authorization': 'Bearer ' + req.cookies.apitoken
},
json: finalArr,
}];
I having problem with making a API call to an external site.
I need to send a POST request to http://api.turfgame.com/v4/users with headers Content-type: application/js. But when I run this code it only loads and nothing more.
var request = require('request');
var options = {
uri: 'http://api.turfgame.com/v4/users',
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: {
"name": "username"
}
};
request(options, function (error, response, body) {
if (!error && response.statusCode == 200) {
res.send(response.body);
}
});
The body need to be posted in json formate [{'name': 'username'}].
Can someone tell me what I have done wrong?
There are a few things wrong:
the address property in the "options" object should be "url" not
"uri"
you can use the "json" property instead of body
"res" is undefined in your response handler function
if you want the body to be an array, it needs to be surrounded in square brackets
here's a working sample that just logs the response:
var request = require('request');
var options = {
url: 'http://api.turfgame.com/v4/users',
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
json: [{
"name": "username"
}]
};
request(options, function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log(response.body);
}
});