Skip to content

Instantly share code, notes, and snippets.

@serinth
Last active September 5, 2024 20:37
Show Gist options
  • Save serinth/6262f85fd38c030269862ea8cecc6606 to your computer and use it in GitHub Desktop.
Save serinth/6262f85fd38c030269862ea8cecc6606 to your computer and use it in GitHub Desktop.
Curl Examples

Show Request and Response

curl -v www.example.com

HEADERS

curl --header "X-Signature: XXX" http://example.com/endpoint

curl -H "X-Signature: XXX" -H "Content-Type: application/json http://example.com/endpoint

VERBS

curl -X DELETE example.com

Ignore SSL Errors

curl --insecure https://example.com

POST JSON Body

curl -H "Content-Type: application/json" -X POST -d '{"field1":"val1","field2":"val2"}' http://localhost:3000/api/test

POST Forms

When:

<form method="POST" action="page.cgi">
  <input type=text name="field1">
  <input type=submit name="Submit" value="OK">
</form>

Then: curl --data "field1=somevalue&Submit=OK" http://www.example.com/page.cgi

or

curl --data-urlencode "field1=value with spaces" http://www.example.com/page.cgi

When:

<form method="POST" enctype='multipart/form-data' action="upload.cgi">
  <input type=file name=upload>
  <input type=submit name="Submit" value="OK">
</form> 

Then: curl --form upload=@localfilename --form Submit=OK http://www.example.com/upload.cgi

Authentication

HTTP Auth

curl -u username:password URL

FTP File Download

curl -u ftpuser:ftppass -O ftp://ftpserver/file.zip

FTP File Upload

curl -u ftpuser:ftppass -T myfile.txt ftp://ftp.testserver.com

OAuth2.0 - Grant Type = Password

For Salesforce you must first register a connected app. Append the security token to the password.

curl -X POST 'https://test.salesforce.com/services/oauth2/token?grant_type=password&username=<ACCOUNT USERNAME>&password=<ACCOUNT PASSWORD><SECURITY TOKEN>&client_id=<CLIENT ID>&client_secret=<SECRET>'

Response:

{
	"access_token":"TOKENVALUE",
	"instance_url":"https://url.com",
	"id":"https://test.salesforce.com/id/xxxxxxxx/xxxxxxxxx",
	"token_type":"Bearer",
	"issued_at":"1477526642560",
	"signature":"nrFRBW0zTYxxxmP0cFAmA3Mo3rlxxxxqeAupBGGeRXuAn0="
}

Make an authenticated request after getting the token

curl -H 'Authorization: Bearer <TOKEN>' -H 'content-length: 0' -X POST 'instance_url/resource'

Crawling

Following Redirects

curl -L http://www.google.com

Limit Rate

curl --limit-rate 1000B -O http://www.example.com/file

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment