How to fetch any API using URL by passing some value using form and get response according to form data from...












0














everyone, I am a beginner in nodejs and expressjs. So while practicing I got a question in my mind is that I am having one URL from where I want to fetch data and I simply did it.
Following is the code:



var express = require('express');
var app = express();
var request = require('request');
var bodyParser = require('body-parser');
app.set('view engine', 'ejs');
var urlencoderparser = bodyParser.urlencoded({ extended : true });
var data;
app.get('/', function (req, res) {
request.get('http://mysafeinfo.com/api/data?list=englishmonarchs&format=json', function(err, res, body){
if(err) {
return console.log(error);
}
data = JSON.parse(body);
});
res.render('index', {data: data});
console.log(data);
});
app.listen(3000);


And this code is working fine.



Then I thought to change the URL by passing values using form.



enter image description here



we can enter a value in the form. The value should be either XML or JSON and according to that it will change the URL like the code given below:



var url = 'http://mysafeinfo.com/api/data?list=englishmonarchs&format='+form_data


Now for this, I tried request.post() in this way:



app.get('/', function (req, res) {
Request.post({
"headers": { "content-type": "application/json" },
"url": "http://mysafeinfo.com/api/data?list=englishmonarchs&format="+form_data,
}, (error, response, body) => {
if(error) {
return console.log(error);
}
data = JSON.parse(body);
});
res.render('index', {data: data});
});
app.listen(3000);


But this is not working.
Please help me to know where I am wrong and whether this way is correct for coding in nodejs or expressjs?



Hello everyone i am adding whole code with you for more clearance



var express = require('express');
var app = express();
var request = require('request');
var bodyParser = require('body-parser');

app.set('view engine', 'ejs');

var urlencoderparser = bodyParser.urlencoded({ extended : true });

app.get('/', function(req, res){
res.render('index');
});

app.post('/', urlencoderparser, function(req, res){
res.render('form-data', {data : req.body});
request.post({
"headers": { "content-type": "application/json" },
"url": "http://mysafeinfo.com/api/data?list=englishmonarchs&format="+req.body.name,
}, (error, response, body) => {
if(error) {
return console.log(error);
}
data = JSON.parse(body);
});
console.log(req.body);
});

app.listen(3000);









share|improve this question
























  • what is the error you are getting ?
    – Chirag Sharma
    Nov 19 '18 at 12:55






  • 1




    What type of error did you get? For the POST example. Is it in an another file or at the same where GET request? For the first look you require var request = require('request'); but in the GET request you use request with a lowercase and in the POST you use with a camelcase.
    – wkornilow
    Nov 19 '18 at 12:57












  • I'm sorry, but is form_data defined anywhere? Shouldn't you use something like req.body.form_data rather than simply form_data?
    – mur762
    Nov 19 '18 at 14:18










  • @ChiragSharma @wkornilow i am getting this error "System.Collections.Generic.Dictionary`2[System.String,System.String]"
    – Abhishek Rawal
    Nov 20 '18 at 7:25










  • @mur762 Tried this but giving error.
    – Abhishek Rawal
    Nov 20 '18 at 7:26
















0














everyone, I am a beginner in nodejs and expressjs. So while practicing I got a question in my mind is that I am having one URL from where I want to fetch data and I simply did it.
Following is the code:



var express = require('express');
var app = express();
var request = require('request');
var bodyParser = require('body-parser');
app.set('view engine', 'ejs');
var urlencoderparser = bodyParser.urlencoded({ extended : true });
var data;
app.get('/', function (req, res) {
request.get('http://mysafeinfo.com/api/data?list=englishmonarchs&format=json', function(err, res, body){
if(err) {
return console.log(error);
}
data = JSON.parse(body);
});
res.render('index', {data: data});
console.log(data);
});
app.listen(3000);


And this code is working fine.



Then I thought to change the URL by passing values using form.



enter image description here



we can enter a value in the form. The value should be either XML or JSON and according to that it will change the URL like the code given below:



var url = 'http://mysafeinfo.com/api/data?list=englishmonarchs&format='+form_data


Now for this, I tried request.post() in this way:



app.get('/', function (req, res) {
Request.post({
"headers": { "content-type": "application/json" },
"url": "http://mysafeinfo.com/api/data?list=englishmonarchs&format="+form_data,
}, (error, response, body) => {
if(error) {
return console.log(error);
}
data = JSON.parse(body);
});
res.render('index', {data: data});
});
app.listen(3000);


But this is not working.
Please help me to know where I am wrong and whether this way is correct for coding in nodejs or expressjs?



Hello everyone i am adding whole code with you for more clearance



var express = require('express');
var app = express();
var request = require('request');
var bodyParser = require('body-parser');

app.set('view engine', 'ejs');

var urlencoderparser = bodyParser.urlencoded({ extended : true });

app.get('/', function(req, res){
res.render('index');
});

app.post('/', urlencoderparser, function(req, res){
res.render('form-data', {data : req.body});
request.post({
"headers": { "content-type": "application/json" },
"url": "http://mysafeinfo.com/api/data?list=englishmonarchs&format="+req.body.name,
}, (error, response, body) => {
if(error) {
return console.log(error);
}
data = JSON.parse(body);
});
console.log(req.body);
});

app.listen(3000);









share|improve this question
























  • what is the error you are getting ?
    – Chirag Sharma
    Nov 19 '18 at 12:55






  • 1




    What type of error did you get? For the POST example. Is it in an another file or at the same where GET request? For the first look you require var request = require('request'); but in the GET request you use request with a lowercase and in the POST you use with a camelcase.
    – wkornilow
    Nov 19 '18 at 12:57












  • I'm sorry, but is form_data defined anywhere? Shouldn't you use something like req.body.form_data rather than simply form_data?
    – mur762
    Nov 19 '18 at 14:18










  • @ChiragSharma @wkornilow i am getting this error "System.Collections.Generic.Dictionary`2[System.String,System.String]"
    – Abhishek Rawal
    Nov 20 '18 at 7:25










  • @mur762 Tried this but giving error.
    – Abhishek Rawal
    Nov 20 '18 at 7:26














0












0








0







everyone, I am a beginner in nodejs and expressjs. So while practicing I got a question in my mind is that I am having one URL from where I want to fetch data and I simply did it.
Following is the code:



var express = require('express');
var app = express();
var request = require('request');
var bodyParser = require('body-parser');
app.set('view engine', 'ejs');
var urlencoderparser = bodyParser.urlencoded({ extended : true });
var data;
app.get('/', function (req, res) {
request.get('http://mysafeinfo.com/api/data?list=englishmonarchs&format=json', function(err, res, body){
if(err) {
return console.log(error);
}
data = JSON.parse(body);
});
res.render('index', {data: data});
console.log(data);
});
app.listen(3000);


And this code is working fine.



Then I thought to change the URL by passing values using form.



enter image description here



we can enter a value in the form. The value should be either XML or JSON and according to that it will change the URL like the code given below:



var url = 'http://mysafeinfo.com/api/data?list=englishmonarchs&format='+form_data


Now for this, I tried request.post() in this way:



app.get('/', function (req, res) {
Request.post({
"headers": { "content-type": "application/json" },
"url": "http://mysafeinfo.com/api/data?list=englishmonarchs&format="+form_data,
}, (error, response, body) => {
if(error) {
return console.log(error);
}
data = JSON.parse(body);
});
res.render('index', {data: data});
});
app.listen(3000);


But this is not working.
Please help me to know where I am wrong and whether this way is correct for coding in nodejs or expressjs?



Hello everyone i am adding whole code with you for more clearance



var express = require('express');
var app = express();
var request = require('request');
var bodyParser = require('body-parser');

app.set('view engine', 'ejs');

var urlencoderparser = bodyParser.urlencoded({ extended : true });

app.get('/', function(req, res){
res.render('index');
});

app.post('/', urlencoderparser, function(req, res){
res.render('form-data', {data : req.body});
request.post({
"headers": { "content-type": "application/json" },
"url": "http://mysafeinfo.com/api/data?list=englishmonarchs&format="+req.body.name,
}, (error, response, body) => {
if(error) {
return console.log(error);
}
data = JSON.parse(body);
});
console.log(req.body);
});

app.listen(3000);









share|improve this question















everyone, I am a beginner in nodejs and expressjs. So while practicing I got a question in my mind is that I am having one URL from where I want to fetch data and I simply did it.
Following is the code:



var express = require('express');
var app = express();
var request = require('request');
var bodyParser = require('body-parser');
app.set('view engine', 'ejs');
var urlencoderparser = bodyParser.urlencoded({ extended : true });
var data;
app.get('/', function (req, res) {
request.get('http://mysafeinfo.com/api/data?list=englishmonarchs&format=json', function(err, res, body){
if(err) {
return console.log(error);
}
data = JSON.parse(body);
});
res.render('index', {data: data});
console.log(data);
});
app.listen(3000);


And this code is working fine.



Then I thought to change the URL by passing values using form.



enter image description here



we can enter a value in the form. The value should be either XML or JSON and according to that it will change the URL like the code given below:



var url = 'http://mysafeinfo.com/api/data?list=englishmonarchs&format='+form_data


Now for this, I tried request.post() in this way:



app.get('/', function (req, res) {
Request.post({
"headers": { "content-type": "application/json" },
"url": "http://mysafeinfo.com/api/data?list=englishmonarchs&format="+form_data,
}, (error, response, body) => {
if(error) {
return console.log(error);
}
data = JSON.parse(body);
});
res.render('index', {data: data});
});
app.listen(3000);


But this is not working.
Please help me to know where I am wrong and whether this way is correct for coding in nodejs or expressjs?



Hello everyone i am adding whole code with you for more clearance



var express = require('express');
var app = express();
var request = require('request');
var bodyParser = require('body-parser');

app.set('view engine', 'ejs');

var urlencoderparser = bodyParser.urlencoded({ extended : true });

app.get('/', function(req, res){
res.render('index');
});

app.post('/', urlencoderparser, function(req, res){
res.render('form-data', {data : req.body});
request.post({
"headers": { "content-type": "application/json" },
"url": "http://mysafeinfo.com/api/data?list=englishmonarchs&format="+req.body.name,
}, (error, response, body) => {
if(error) {
return console.log(error);
}
data = JSON.parse(body);
});
console.log(req.body);
});

app.listen(3000);






javascript node.js api express ejs






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 '18 at 7:31

























asked Nov 19 '18 at 12:50









Abhishek Rawal

708




708












  • what is the error you are getting ?
    – Chirag Sharma
    Nov 19 '18 at 12:55






  • 1




    What type of error did you get? For the POST example. Is it in an another file or at the same where GET request? For the first look you require var request = require('request'); but in the GET request you use request with a lowercase and in the POST you use with a camelcase.
    – wkornilow
    Nov 19 '18 at 12:57












  • I'm sorry, but is form_data defined anywhere? Shouldn't you use something like req.body.form_data rather than simply form_data?
    – mur762
    Nov 19 '18 at 14:18










  • @ChiragSharma @wkornilow i am getting this error "System.Collections.Generic.Dictionary`2[System.String,System.String]"
    – Abhishek Rawal
    Nov 20 '18 at 7:25










  • @mur762 Tried this but giving error.
    – Abhishek Rawal
    Nov 20 '18 at 7:26


















  • what is the error you are getting ?
    – Chirag Sharma
    Nov 19 '18 at 12:55






  • 1




    What type of error did you get? For the POST example. Is it in an another file or at the same where GET request? For the first look you require var request = require('request'); but in the GET request you use request with a lowercase and in the POST you use with a camelcase.
    – wkornilow
    Nov 19 '18 at 12:57












  • I'm sorry, but is form_data defined anywhere? Shouldn't you use something like req.body.form_data rather than simply form_data?
    – mur762
    Nov 19 '18 at 14:18










  • @ChiragSharma @wkornilow i am getting this error "System.Collections.Generic.Dictionary`2[System.String,System.String]"
    – Abhishek Rawal
    Nov 20 '18 at 7:25










  • @mur762 Tried this but giving error.
    – Abhishek Rawal
    Nov 20 '18 at 7:26
















what is the error you are getting ?
– Chirag Sharma
Nov 19 '18 at 12:55




what is the error you are getting ?
– Chirag Sharma
Nov 19 '18 at 12:55




1




1




What type of error did you get? For the POST example. Is it in an another file or at the same where GET request? For the first look you require var request = require('request'); but in the GET request you use request with a lowercase and in the POST you use with a camelcase.
– wkornilow
Nov 19 '18 at 12:57






What type of error did you get? For the POST example. Is it in an another file or at the same where GET request? For the first look you require var request = require('request'); but in the GET request you use request with a lowercase and in the POST you use with a camelcase.
– wkornilow
Nov 19 '18 at 12:57














I'm sorry, but is form_data defined anywhere? Shouldn't you use something like req.body.form_data rather than simply form_data?
– mur762
Nov 19 '18 at 14:18




I'm sorry, but is form_data defined anywhere? Shouldn't you use something like req.body.form_data rather than simply form_data?
– mur762
Nov 19 '18 at 14:18












@ChiragSharma @wkornilow i am getting this error "System.Collections.Generic.Dictionary`2[System.String,System.String]"
– Abhishek Rawal
Nov 20 '18 at 7:25




@ChiragSharma @wkornilow i am getting this error "System.Collections.Generic.Dictionary`2[System.String,System.String]"
– Abhishek Rawal
Nov 20 '18 at 7:25












@mur762 Tried this but giving error.
– Abhishek Rawal
Nov 20 '18 at 7:26




@mur762 Tried this but giving error.
– Abhishek Rawal
Nov 20 '18 at 7:26

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53375029%2fhow-to-fetch-any-api-using-url-by-passing-some-value-using-form-and-get-response%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53375029%2fhow-to-fetch-any-api-using-url-by-passing-some-value-using-form-and-get-response%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

SQL update select statement

'app-layout' is not a known element: how to share Component with different Modules