send Content-Type: application/json post with node.js

Mikeal’s request module can do this easily: var request = require(‘request’); var options = { uri: ‘https://www.googleapis.com/urlshortener/v1/url’, method: ‘POST’, json: { “longUrl”: “http://www.google.com/” } }; request(options, function (error, response, body) { if (!error && response.statusCode == 200) { console.log(body.id) // Print the shortened url. } });

Set cURL to use local virtual hosts

Actually, curl has an option explicitly for this: –resolve Instead of curl -H ‘Host: yada.com’ http://127.0.0.1/something use curl –resolve ‘yada.com:80:127.0.0.1′ http://yada.com/something What’s the difference, you ask? Among others, this works with HTTPS. Assuming your local server has a certificate for yada.com, the first example above will fail because the yada.com certificate doesn’t match the 127.0.0.1 … Read more

Curl with multiline of JSON

I remembered another way to do this with a “Here Document” as described in the Bash man page and detailed here. The @- means to read the body from STDIN, while << EOF means to pipe the script content until “EOF” as STDIN to curl. This layout may be easier to read than using separate … Read more

Is there a way to give a specific file name when saving a file via cURL?

Either use the -o option or its alias –output, or redirect shell output to the file of choice by using >. curl -o /path/to/local/file http://url.com curl http://url.com > /path/to/local/file If you want to preserve the original file name from the remote server, use the -O option or its alias –remote-name. curl -O http://url.com/file.html Stores the … Read more

How to use CURL via a proxy?

Here is a working version with your bugs removed. $url=”http://dynupdate.no-ip.com/ip.php”; $proxy = ‘127.0.0.1:8888’; //$proxyauth=”user:password”; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL,$url); curl_setopt($ch, CURLOPT_PROXY, $proxy); //curl_setopt($ch, CURLOPT_PROXYUSERPWD, $proxyauth); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_HEADER, 1); $curl_scraped_page = curl_exec($ch); curl_close($ch); echo $curl_scraped_page; I have added CURLOPT_PROXYUSERPWD in case any of your proxies require a user name and … Read more

how to get the cookies from a php curl into a variable

$ch = curl_init(‘http://www.google.com/’); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // get headers too with this line curl_setopt($ch, CURLOPT_HEADER, 1); $result = curl_exec($ch); // get cookie // multi-cookie variant contributed by @Combuster in comments preg_match_all(‘/^Set-Cookie:\s*([^;]*)/mi’, $result, $matches); $cookies = array(); foreach($matches[1] as $item) { parse_str($item, $cookie); $cookies = array_merge($cookies, $cookie); } var_dump($cookies);

CURL to access a page that requires a login from a different page

The web site likely uses cookies to store your session information. When you run curl –user user:pass https://xyz.example/a #works ok curl https://xyz.example/b #doesn’t work curl is run twice, in two separate sessions. Thus when the second command runs, the cookies set by the 1st command are not available; it’s just as if you logged in … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)