Automatically add newline at end of curl response body

From the man file: To better allow script programmers to get to know about the progress of curl, the -w/–write-out option was introduced. Using this, you can specify what information from the previous transfer you want to extract. To display the amount of bytes downloaded together with some text and an ending newline: curl -w … Read more

How to define the basic HTTP authentication using cURL correctly?

curl -u username:password http:// curl -u username http:// From the documentation page: -u, –user <user:password> Specify the user name and password to use for server authentication. Overrides -n, –netrc and –netrc-optional. If you simply specify the user name, curl will prompt for a password. The user name and passwords are split up on the first … Read more

Display curl output in readable JSON format in Unix shell script

A few solutions to choose from: json_pp: command utility available in Linux systems for JSON decoding/encoding echo ‘{“type”:”Bar”,”id”:”1″,”title”:”Foo”}’ | json_pp -json_opt pretty,canonical { “id” : “1”, “title” : “Foo”, “type” : “Bar” } You may want to keep the -json_opt pretty,canonical argument for predictable ordering. jq: lightweight and flexible command-line JSON processor. It is written … Read more

Send request to cURL with post data sourced from a file

You’re looking for the –data-binary argument: curl -i -X POST host:port/post-file \ -H “Content-Type: text/xml” \ –data-binary “@path/to/file” In the example above, -i prints out all the headers so that you can see what’s going on, and -X POST makes it explicit that this is a post. Both of these can be safely omitted without … Read more

Passing a URL with brackets to curl

Add -g to your command: -g, –globoff This option switches off the “URL globbing parser”. When you set this option, you can specify URLs that contain the letters {}[] without having curl itself interpret them. Note that these letters are not normal legal URL contents but they should be encoded according to the URI standard. … Read more

List all indexes on ElasticSearch server?

For a concise list of all indices in your cluster, call curl http://localhost:9200/_aliases this will give you a list of indices and their aliases. If you want it pretty-printed, add pretty=true: curl http://localhost:9200/_aliases?pretty=true The result will look something like this, if your indices are called old_deuteronomy and mungojerrie: { “old_deuteronomy” : { “aliases” : { … Read more

Can PHP cURL retrieve response headers AND body in a single request?

One solution to this was posted in the PHP documentation comments: http://www.php.net/manual/en/function.curl-exec.php#80442 Code example: $ch = curl_init(); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_HEADER, 1); // … $response = curl_exec($ch); // Then, after your curl_exec call: $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE); $header = substr($response, 0, $header_size); $body = substr($response, $header_size); Warning: As noted in the comments below, this … Read more

HTTP POST and GET using cURL in Linux [duplicate]

*nix provides a nice little command which makes our lives a lot easier. GET: with JSON: curl -i -H “Accept: application/json” -H “Content-Type: application/json” -X GET http://hostname/resource with XML: curl -H “Accept: application/xml” -H “Content-Type: application/xml” -X GET http://hostname/resource POST: For posting data: curl –data “param1=value1&param2=value2” http://hostname/resource For file upload: curl –form “fileupload=@filename.txt” http://hostname/resource RESTful … Read more

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