Skip to content

Instantly share code, notes, and snippets.

@fsojitra
Created December 16, 2017 09:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fsojitra/8a2e0de280a8c0d90ef4af1e1f0ba15c to your computer and use it in GitHub Desktop.
Save fsojitra/8a2e0de280a8c0d90ef4af1e1f0ba15c to your computer and use it in GitHub Desktop.
var express = require("express");
var app = express();
var request = require('request');
var options = {
//url:'https://jsonplaceholder.typicode.com/comments',
url:'http://date.jsontest.com/?service='+'ip',
method: 'get',
//path:'/comments',
headers: {
'Accept': 'application/json',
'Accept-Charset': 'utf-8',
'User-Agent': 'my-reddit-client'
}
}
request(/*'https://jsonplaceholder.typicode.com/posts'*/options, function(err, res, body) {
var json = JSON.parse(body);
console.log(json);
//console.log(body);
/*for(var i = 0; i<body.length; i++){
console.log(body[i]);
}
*/});
app.listen(2000, function() {
console.log("Listening on 2000");
});
you can convert curl like this to fire through requst
-#, --progress-bar Make curl display a simple progress bar instead of the more informational standard meter.
-b, --cookie <name=data> Supply cookie with request. If no =, then specifies the cookie file to use (see -c).
-c, --cookie-jar <file name> File to save response cookies to.
-d, --data <data> Send specified data in POST request. Details provided below.
-f, --fail Fail silently (don't output HTML error form if returned)'.
-F, --form <name=content> Submit form data.
-H, --header <header> Headers to supply with request.
-i, --include Include HTTP headers in the output.
-I, --head Fetch headers only.
-k, --insecure Allow insecure connections to succeed.
-L, --location Follow redirects.
-o, --output <file> Write output to . Can use --create-dirs in conjunction with this to create any directories specified in the -o path.
-O, --remote-name Write output to file named like the remote file (only writes to current directory).
-s, --silent Silent (quiet) mode. Use with -S to force it to show errors.
-v, --verbose Provide more information (useful for debugging).
-w, --write-out <format> Make curl display information on stdout after a completed transfer. See man page for more details on available variables. Convenient way to force curl to append a newline to output: -w "\n" (can add to ~/.curlrc).
-X, --request The request method to use.
---
curl https://www.googleapis.com/urlshortener/v1/url \
-H 'Content-Type: application/json' \
-d '{"longUrl": "http://www.google.com/"}'
var headersOpt = {
"content-type": "application/json",
};
request(
{
method:'post',
url:'https://www.googleapis.com/urlshortener/v1/url',
form: {name:'hello',age:25},
headers: headersOpt,
json: true,
}, function (error, response, body) {
//Print the Response
console.log(body);
});
//=====================
var options = {
uri: 'https://www.googleapis.com/urlshortener/v1/url',
method: 'POST',
json: {
"longUrl": "http://www.google.com/"
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment