How to use the post function from superagent
Find comprehensive JavaScript superagent.post code examples handpicked from public code repositorys.
superagent.post is a function in the SuperAgent library that sends an HTTP POST request to a specified URL.
46 47 48 49 50 51 52 53 54 55
apiRoutes.get('/robotapi', (req, res) => { let response=res let info = req.query.message let userid = req.query.id let key = '069e90c4262243bf964ad95014371384' superagent.post('http://www.tuling123.com/openapi/api') .send({info, userid, key}) .end((err,res) => { if(err){ console.log(err)
144 145 146 147 148 149 150 151 152 153
reject(null); } return; } request.post('http://fanyi.baidu.com/langdetect').send('query=' + queryObj.text.slice(0, 73)).end(function (err, res) { if (err) { reject(err); } else { var body = res.body;
+ 3 other calls in file
How does superagent.post work?
superagent.post
is a function in the SuperAgent library that sends an HTTP POST request to a specified URL.
The function takes two arguments: the URL to which the request will be sent, and an optional data payload to be included in the request body.
The data payload can be specified using various formats, including JSON, forms, or files.
Once the superagent.post
function is called, the request is sent to the specified URL with the specified data payload, and the server response is returned as a Promise object.
The Promise object can then be used to handle the response data or any errors that may occur during the request.
Overall, superagent.post
provides a simple and flexible way to send POST requests to a server, making it a valuable tool for building web applications and APIs.
GitHub: FredrikNoren/ungit
132 133 134 135 136 137 138 139 140
let req; if (method === 'GET') { req = request.get(url).withCredentials().query(body); } else if (method === 'POST') { req = request.post(url).send(body); } else if (method === 'DELETE') { req = request.delete(url).send(body); }
63 64 65 66 67 68 69 70 71 72
} success(data); } } apiRequest = request.post(getBaseURL(host) + path) .set('Authorization', 'Bearer ' + accessToken) .set('Dropbox-API-Arg', JSON.stringify(args)) .on('request', function () { if (this.xhr) {
+ 9 other calls in file
Ai Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
const superagent = require("superagent"); const url = "https://myapi.com/data"; const data = { name: "John", age: 30 }; superagent .post(url) .send(data) .set("Accept", "application/json") .then((res) => { console.log(res.body); }) .catch((err) => { console.error(err); });
In this example, we use superagent.post to send a POST request to the URL https://myapi.com/data with a JSON payload containing a name and age property. We use the send method to include the data payload in the request body, and the set method to set the Accept header to application/json. Once the request is sent, we use a Promise chain to handle the response data. If the request is successful, the response body is logged to the console. If an error occurs, the error message is logged to the console instead. This is just one example of how superagent.post can be used to send POST requests with various data formats. With the ability to customize headers and handle responses with Promises, superagent.post provides a powerful tool for interacting with web APIs.
GitHub: DefiLlama/yield-server
67 68 69 70 71 72 73 74 75 76 77
return apy; }; const getPricesByAddresses = async (addresses) => { const prices = ( await superagent.post('https://coins.llama.fi/prices').send({ coins: addresses.map((address) => `${chain}:${address}`), }) ).body.coins;
GitHub: tinderjs/tinderjs
54 55 56 57 58 59 60 61 62 63
* @param {String} path the relative path * @param {Object} data an object containing extra values * @param {Function} callback the callback to invoke when the request completes */ var tinderPost = function(path, data, callback) { request.post(TINDER_HOST + path) .set(getRequestHeaders()) .send(data) .end(callback); };
+ 3 other calls in file
GitHub: stkevintan/Cube
16 17 18 19 20 21 22 23 24 25
} var httpRequest = function (method, url, data, callback) { var ret; if (method == 'post') { ret = request.post(url).send(data); } else { ret = request.get(url).query(data); } var cookie = fm.getCookie();
33 34 35 36 37 38 39 40 41 42
"metrics": chartDetails.api.metrics }, }; return new Promise(resolve => { superagent.post(chartDetails.url + chartDetails.api["end-point"]) .send(json_body) //sends JSON POST body .set({ "Authorization": 'Bearer ' + accessToken, //headers "x-proxy-company": chartDetails.company,
41 42 43 44 45 46 47 48 49 50
if(!username) {return;} const expiry = new Date; expiry.setFullYear(expiry.getFullYear() + 1); const token = await request.post('/local/login') .send({ username }) .then((response)=>{ return response.body; })
+ 5 other calls in file
2338 2339 2340 2341 2342 2343 2344 2345 2346 2347
* * Examples: * * superagent.types.xml = 'application/xml'; * * request.post('/') * .type('xml') * .send(xmlstring) * .end(callback); *
+ 39 other calls in file
78 79 80 81 82 83 84 85 86 87
console.log(user); if (!user.captcharesponse || !user.username || !user.password1 || !user.password2 || user.password1 !== user.password2) { res.status(400).json({ reason: 'Incomplete parameters' }); return; } sa.post('https://www.google.com/recaptcha/api/siteverify') .send({ secret: process.env.RECAPTCHA_SECRET_KEY, response: user.captcharesponse, remoteip: req.connection.remoteAddress
51 52 53 54 55 56 57 58 59 60
* @returns The given user's login cookie * @param {string} email * @param {string} hashedPass */ static async getLoginCookie(email, hashedPass) { return superagent.post(KtuvitManager.KTUVIT.LOGIN_URL) .send({"request" :{Email:email , Password:hashedPass}}).then(res => { //Parsing the cookie as a string because a cookie parser would be // an extra dependency for a single use. return res.headers['set-cookie'][1].split(';')[0].replace('Login=','')
+ 3 other calls in file
694 695 696 697 698 699 700 701 702 703
} // STEP 2: Decode old channel config // configtxlator proto_decode --input config_block.pb --type common.Block | // ... jq .data.data[0].payload.data.config > config.json let oldChannelConfig = await superagent.post(configtxlatorAddr + '/protolator/decode/common.Config', configEnvelope.config.toBuffer()) .then((res) => { return res; }).catch(err => {
+ 5 other calls in file
GitHub: NotARoomba/GrindHub
57 58 59 60 61 62 63 64 65 66
} //set user data randomKeys = prompt("Enter some random letters and numbers for your key (sha256 hash).") alert(`Your secret key is '${sha256(randomKeys)}', please make sure that you copy this key for future login!'`) //add a user await superagent.post(BACKEND_URL + "/signup").send({ key: sha256(randomKeys), email: email, name: name, image: null, strength: 0, defense: 0, intelligence: 0, hasRefreshed: false, completed: [] }).body window.location.href = "index.html" } catch (e) { console.log(e) alert("An error occured! Please try again!")
+ 79 other calls in file
GitHub: TaoEvan/Dyeify
6004 6005 6006 6007 6008 6009 6010 6011 6012 6013 6014
* @param {BaseRequest} The request. * @param {Function} The callback function. */ HttpManager.post = function(request, callback) { var options = _getParametersFromRequest(request); var method = superagent.post; HttpManager._makeRequest(method, options, request.getURI(), callback); };
+ 3 other calls in file
19 20 21 22 23 24 25 26 27 28
describe(".login", () => { const url = `${__API_URL__}/sessions`; context("with valid parameter", () => { beforeEach(() => { mock.post(url, () => ({ body: { email: "test@example.com", }, }));
+ 15 other calls in file
9 10 11 12 13 14 15 16 17 18
// 参数 i 为请求的页码数,包括开始也和结束页 setUrl(5); var arr = []; function setUrl(i) { console.log('开始请求数据页码数据'); superagent.post('http://www.bidizhaobiao.com/advsearch/retrieval_list.do') .type('form') .set({ 'Content-Type':'application/x-www-form-urlencoded', 'Referer':'http://www.bidizhaobiao.com/advsearch/retrieval_list.do',
98 99 100 101 102 103 104 105 106 107 108 109 110 111
} // superagent.post('http://www.bidizhaobiao.com/advsearch/retrieval_list.do') // .type('form') // .set({ // 'Referer':'http://www.bidizhaobiao.com/advsearch/retrieval_list.do', // 'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8'
38 39 40 41 42 43 44 45 46 47
} console.log(arr); // // 遍历 arr, 解析每个页面中需要的信息 async.mapLimit(arr, 3, function (url, callback) { var jsonData = []; superagent.post(url).end(function (err, mes) { if (err) { console.log("get \""+url+"\" error !"+err); console.log("message info:"+JSON.stringify(mes)); }
+ 2 other calls in file
superagent.get is the most popular function in superagent (1221 examples)