Skip to main content

Posts

Upload a multi-part/image/file using curl

This post is about uploading a file to server using curl. Why I'm writing this post is because of a possible bug in Postman which forced me to look for an alternative to test my upload api.


Test if curl is present in system :
$ which curl
/usr/bin/curl

If curl is not present then install it : yum (centos), apt-get (ubuntu), brew (mac)

Syntax of the command :
curl -F "param1=value" -F "param2=value2" -F "filecomment=file_comment" -F "image=@image_path" server_api


Here's an example of the same :
curl -F "user_id=12" -F "description": "desc" -F "filecomment=This is an image file" -F "image=@/Users/Desktop/testim.png" localhost:80/myblog/create

If using nodeJs, you can fetch the values in fields & files parameters.
Recent posts

configuring apache for python

If you have basic understanding of apache, then this article is highly recommended for setup:
http://httpd.apache.org/docs/1.3/howto/cgi.html


I did setup this in my mac/OSx, was not easy to find info on web, hence I am briefing out the flow:
1) uncomment "LoadModule cgi_module libexec/apache2/mod_cgi.so" ( for me it exists in httpd.conf )
2) edit virtual hosts file to look like this: ( for mac, it is in httpd-vhosts.conf )

Options Indexes MultiViews FollowSymLinks ExecCGI
AllowOverride All
Order allow,deny
Allow from all


NOTE: "/www" is where my project resides
"ExecCGI" is the option that has to be there
3) Below the directory closing, add this line: ( for mac, it is in httpd-vhosts.conf )
AddHandler cgi-script cgi py

4) Restart apache: "sudo apachectl restart"

Now running your project should give "End of script output before headers" error in error log and 500 internal se…

delete all the values with in a group keeping only the topmost entry in each group in mysql

sometimes you're caught up in a situation where you have multiple entries against an id and you want to keep only single entry against each.

Table structure would be something like this:

Table `temp`

Entries:
user_id tstamp source
100 4/26/2015 app
100 4/26/2014 web
100 4/25/2015 app
101 4/26/2015 app
100 4/23/2015 app
101 4/20/2015 web
100 4/1/2015 app
200 4/26/2015 app
200 4/2/2015 app

Desired Output:
user_id tstamp source
100 4/26/2015 app
100 4/26/2014 web
101 4/26/2015 app
200 4/26/2015 app


Now, I want to delete the rows where source is `app` and keep the entry with max tstamp only with in a group.
Here's the sample query to achieve the same:

DELETE tmp FROM `temp` tmp join (select user_id, max(tstamp) as ts from `temp` where user_id in (select user_id from `temp` where source = 'app' group by user_id having count(user_id)>2) and source = 'app' group by user_id )t on tmp.user_id = t.user_id and tmp.tstamp != t.ts and tmp.source = 'app&…

how to access a private member outside the class in php (php Hack)

If you want to access a private variable/method outside the class... here is a trick to do the same:

class A
{
private $notVisibleOutside = 1;
}


$a = new ReflectionClass("A");
$property = $class->getProperty("notVisibleOutside ");
$property->setAccessible(true);

$a1 = new A();
echo $property->getValue($a1); // Work

So, the basic idea is to
- create a reflection class in php
- make the property or method accessible
- use that property to access the private variables/methods

update issue in mysql

update table temp set `a`=`a`+1 , `b`= `a`+ 1;

this query sometimes doesn't yield the result we are actually looking for..
so the problem is `a` is getting updated first.

So, the actual query should be :
update table temp set `b`= `a`+ 1, `a`=`a`+1 ;

How to do mass insert in redis

A basic and fairly easy way to do mass insert in redis:

Command to use: echo -e "$(cat redis_mass_insert.txt)" | redis-cli --pipe
Content of file "redis_mass_insert.txt": *3\r\n$5\r\nlpush\r\n$5\r\nu:m:1\r\n$5\r\nvalu1\r\n*3\r\n$5\r\nlpush\r\n$5\r\nu:m:1\r\n$7\r\nmyvalue\r\n

Result: All data transferred. Waiting for the last reply...
ERR unknown command ' *2'
ERR unknown command '$4'
ERR wrong number of arguments for 'echo' command
ERR unknown command '$20'
ERR unknown command ' A�j d�Q;yT��าก �h>'

NOTE: command line will show error but in actual the data is transferred and you can check it by entering in redis.

Here's the snapshot of the whole procedure:




To check how the command is responding: use hexdump -C
Command: echo -n $'$3\r\nset\r\n$3\r\nkey\r\n$5\r\nvalue\r\n ' | hexdump -C
whose output will be something like
00000000 24 33 0d 0a 73 65 74 0d 0a 24 33 0d 0a 6b 65 79 |$3..set..$3..key|
00000010 0d 0a…