WRITELOOP

CURL CHEATSHEET

2020 May 4

MAKE A POST REQUEST PASSING PARAMETERS:

  • Single param: $ curl -d ‘param1=123456’ http://localhost:8000/token_auth/request_authorization/; (if it is a POST to a Django URL, always put the bar on the end of the URL)
  • Many params: $ curl -d “param1=valor1” -d “param2=valor2” -d “param3=valor3” http://domain.request.com

AUTHENTICATED CALLS (THROUGH COOKIES):

To make authenticated calls you must have a session cookie (and/or be logged on the system). To get this cookie I could use Chrome’s Developer Tools and get the session cookie, on the Network tab, clicking on any method. After getting that cookies, I must make my calls and insert the “-b” parameters with the cookie contents. E.g.: $ curl -b “cookieexxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxyyyyyyyyyyyyyyzzzzzzzzzzzzzz” -d “var=value” Chrome Developer Tools request headers e.g.: Accept:text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8 Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3 Accept-Encoding:gzip,deflate,sdch Accept-Language:en-US,en;q=0.8 Cache-Control:max-age=0 Connection:keep-alive Content-Length:84 Content-Type:application/x-www-form-urlencoded Cookie:SLB=c9142c02c234d403519382060050; symfony=14sm3adsfeqf7p6srsh23u0c43; module=ltccqtdsp94h9nig2sa4i39b77 Host:webmail.com.br Origin:http://webmail.com.br Referer:http://webmail.com.br/ User-Agent:Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.172 Safari/537.22

GET HTTP RESPONSE HEADERS (just the “headers”):

$ curl -s -d ’login=user&lpassword=123456’ -D - ‘http://dominio.com’ -o /dev/null (It’s the “-o” that removes the response body and gets just the headers. If I remove the “-o”, I get everything. In the case of symfony, and maybe Django, that is useful e.g. to get just the session cookie. )

HOW TO SEND A FILE (for example, a XML file for a SOAP Webservice):

$ curl -X POST -d @my_file.xml ‘http://localhost:8000/webservice/my_method/’ … and how to get the file contents through a DJANGO view: def insert_entity(request):

Django before 1.4:

print ‘REQUEST RAW POST DATA…: %s’ % repr(request.raw_post_data)

Django after 1.4:

print ‘REQUEST BODY…: %s’ % repr(request.body)

HOW TO SEND JSON PARAMETERS “INLINE”:

$ curl -i -H “Content-Type: application/json;charset=utf-8” -X POST -d ‘{“sku”:{“id”:“999”,“sku”:“999”,“description”:“tênnis naike tamanho 40”,“gender”:“masc.”,“extra_fields”:{“anytext”:“Camizeta NAIK”}}}’ http://localhost:5001/normalize

HOW TO SEND TWO PARAMETERS ON THE REQUEST HEADERS (-H):

$ curl -i -H ‘Content-Type: application/json;charset=utf-8’ -H ‘PRIVATE-TOKEN: 1fUbmQBJeLKaN5VKJbCd’ -X POST -d ‘{“id”: 2, “password”:“pym@ArkG1tl@B”, “email”:“test@example.com”, “username”: “pymark”, “name”: “pymark gitlab user”, “admin”: “true”}’ http://gitlab:10080/api/v3/users

IF-MODIFIED-SINCE HEADER:

$ curl -I –header ‘If-Modified-Since: Tue, 13 Jan 2015 15:03:00 GMT’ http://localhost:8080/lisa/synonyms/all (parameter “-I” will ask just for the response header)

SHOW TIME SPENT ON EACH STAGE OF A REQUEST:

For that, first you create a text file with the required request metrics you want to inspect the timing and then you make the request with curl.

$ vim curl-format.txt
time_namelookup: %{time_namelookup}\n
time_connect: %{time_connect}\n
time_appconnect: %{time_appconnect}\n
time_pretransfer: %{time_pretransfer}\n
time_redirect: %{time_redirect}\n
time_starttransfer: %{time_starttransfer}\n
———
time_total: %{time_total}\n
$ curl -w "@curl-format.txt" -o /dev/null -s google.com

That is an example output:

$ curl -w "@curl-format.txt" -o /dev/null -s google.com
time_namelookup: 0,012520
time_connect: 0,016508
time_appconnect: 0,000000
time_pretransfer: 0,016608
time_redirect: 0,000000
time_starttransfer: 0,072414
———time_total: 0,072719