本文整理汇总了TypeScript中needle.post函数的典型用法代码示例。如果您正苦于以下问题:TypeScript post函数的具体用法?TypeScript post怎么用?TypeScript post使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了post函数的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: Multipart
function Multipart() {
const buffer = fs.readFileSync('/path/to/package.zip');
const data = {
zip_file: {
buffer,
filename: 'mypackage.zip',
content_type: 'application/octet-stream'
}
};
// using promises
needle('post', 'http://somewhere.com/over/the/rainbow', data, { multipart: true })
.then((resp) => {
// if you see, when using buffers we need to pass the filename for the multipart body.
// you can also pass a filename when using the file path method, in case you want to override
// the default filename to be received on the other end.
});
// using callback
needle.post('http://somewhere.com/over/the/rainbow', data, { multipart: true }, (err, resp, body) => {
// if you see, when using buffers we need to pass the filename for the multipart body.
// you can also pass a filename when using the file path method, in case you want to override
// the default filename to be received on the other end.
});
}
示例2: API_post
function API_post() {
var options = {
headers: { 'X-Custom-Header': 'Bumbaway atuna' }
};
needle.post('https://my.app.com/endpoint', 'foo=bar', options, function (err, resp) {
// you can pass params as a string or as an object.
});
}
示例3: FileUpload
function FileUpload() {
var data = {
foo: 'bar',
image: { file: '/home/tomas/linux.png', content_type: 'image/png' }
};
needle.post('http://my.other.app.com', data, { multipart: true }, function(err, resp, body) {
// needle will read the file and include it in the form-data as binary
});
needle.put('https://api.app.com/v2', fs.createReadStream('myfile.txt'), function(err, resp, body) {
// stream content is uploaded verbatim
});
}
示例4: function
inviteUserToSlack: function(user) {
var url = 'https://' + sails.config.slack.communityName + '.slack.com/api/users.admin.invite?t=' + Date.now();
var data = {
email: user.email,
channels: "C051QDDUU,C051QDJJN",
first_name: user.username,
token: sails.config.slack.token,
set_active: true
};
Needle.post(url, data, function(err, resp) {
if (err) PrettyError(err, "An error occurred while inviting a user to slack:")
});
}
示例5: MultipartContentType
function MultipartContentType() {
var data = {
token: 'verysecret',
payload: {
value: JSON.stringify({ title: 'test', version: 1 }),
content_type: 'application/json'
}
}
needle.post('http://test.com/', data, { timeout: 5000, multipart: true }, function(err, resp, body) {
// in this case, if the request takes more than 5 seconds
// the callback will return a [Socket closed] error
});
}
示例6: API_post
function API_post() {
const options = {
headers: { 'X-Custom-Header': 'Bumbaway atuna' }
};
// using promises
needle('post', 'https://my.app.com/endpoint', 'foo=bar', options)
.then((resp) => {
// you can pass params as a string or as an object.
});
// using callback
needle.post('https://my.app.com/endpoint', 'foo=bar', options, (err, resp) => {
// you can pass params as a string or as an object.
});
}
示例7: publishWebhook
function publishWebhook(message: Message, url: string) {
const options = {
json: true,
};
needle.post(url, message, options, (err, res) => {
if (err) {
return false;
}
if (res.body && res.body.error) {
return false;
}
return true;
});
}
示例8: sendContactEmail
export function sendContactEmail(req, res) {
const DOMAIN = 'sandbox587d48a9dc7c4ef696a3a149c2d9747d.mailgun.org';
let KEY = 'key-998712bea03a50212c650813b823352f';
let options = req.body;
let recipient = (process.env.NODE_ENV !== 'production') ?
'qiyen77@gmail.com' :
['proair@proairmarine.com','shiamary@proairmarine.com'];
needle.post(`https://api:${KEY}@api.mailgun.net/v3/${DOMAIN}/messages`, {
from: options.email,
to: recipient,
subject: `Email from ${options.name} titled: ${options.title}`,
text: options.message,
}, (err, response) => (err) ?
res.status(200).json({ sent: false, data: err }) :
res.status(200).json({ sent: true, data: response.body }));
}
示例9: function
}).then(function (transaction:Transaction) {
Needle.post(transaction.paypalExecuteUrl, {
payer_id: payerId
}, function (err, resp) {
if (err) {
PrettyError(err, "An error occurred during Needle.post inside AddonsController.paypalCheckoutGET:");
req.flash('error', "Something went wrong during PayPal checkout, please try again.");
res.redirect('/addons/' + addonId)
} else {
Addon.findOne(addonId)
.then(function (addon:Addon) {
transaction.inProgress = false;
req.user.sentTransactions.add(transaction);
addon.author.receivedTransactions.add(transaction);
// If the user bought a paid addon we need to track the purchase
if (addon.price > 0) {
req.user.purchases.add(addon);
}
if (transaction.couponCode !== undefined) addon.incrementCoupon(transaction.couponCode);
addon.author.balance += amountToCharge;
return [addon, req.user.save(), addon.author.save(), transaction.save()];
}).spread(function (addon) {
if (addon.price === 0) {
NotificationService.sendUserNotification(addon.author, "MEDIUM", req.user.username + " donated $" + transaction.amount / 100 + " USD to your addon, '" + addon.name);
req.flash('success', 'Donation success');
} else {
NotificationService.sendUserNotification(addon.author, "MEDIUM", req.user.username + " purchased your addon, '" + addon.name + "' for $" + transaction.amount / 100);
req.flash('success', 'Purchase success');
}
FeeService.chargeFee(addon.author);
res.redirect('/addons/' + addonId);
}).catch(function () {
PrettyError(err, "An error occurred during Addon.findOne inside AddonsController.paypalCheckoutGET:");
req.flash('warning', "Something went wrong but your purchase succeeded. You may be contacted by support.");
res.redirect('/addons/' + addonId)
})
}
})
}).catch(function (err) {
示例10: FileUpload
function FileUpload() {
const data = {
foo: 'bar',
image: { file: '/home/tomas/linux.png', content_type: 'image/png' }
};
// using promises
needle('post', 'http://my.other.app.com', data, { multipart: true })
.then((resp) => {
// needle will read the file and include it in the form-data as binary
});
needle('put', 'https://api.app.com/v2', fs.createReadStream('myfile.txt'))
.then((resp) => {
// stream content is uploaded verbatim
});
// using callback
needle.post('http://my.other.app.com', data, { multipart: true }, (err, resp, body) => {
// needle will read the file and include it in the form-data as binary
});
needle.put('https://api.app.com/v2', fs.createReadStream('myfile.txt'), (err, resp, body) => {
// stream content is uploaded verbatim
});
}