What is CURL?
Tell me more about CURL
CURL AKA Client URL.
It is a Command Line Tool used by developers to transfer and receive data from Server.
CURL supports different protocol including HTTP & HTTPS.
so now it is much easier to test communication from local server to edge device.
The most basic command in curl is curl “URL”
$ curl https://ahedbahri.social
This will show you the content of the page in HTML.
It is the same default command curl GET “URL”
$ curl GET https://ahedbahri.social
But what if I wanna see more information about the website -I option will do the job.
$ curl -I https://ahedbahri.social
Cool, now Let’s spice things up...
I wanna send a post request to the same URL! Or actually not that URL because it doesn’t contain any form.
will use this one instead: https://reqbin.com/echo/post/json
$ curl POST https://reqbin.com/echo/post/json
is it creepy that we didn’t get any success?? Definitely not
How about we add -X ( so we can announce that we are making a custom request then we specify the HTTP method )
$ curl -X POST https://reqbin.com/echo/post/json
By the way, you should be familiar with ( GET, POST, PUT, DELETE)
GET: is to retrieve data from the server
POST: to send data to the server
PUT: to Create or update a record in the Database or content of a file
DELETE: To delete a resource such as a Database
Well back to reality
It makes no sense to send an empty post request! So we must include headers, Authorization Token, Type of content to receive or send, OR ALL of what we just mentioned.
let’s try once again
$ curl -X POST https://reqbin.com/echo/post/json -H ‘Content-Type: application/json’
- - H we are saying that we wish to add a header to our post request
$ curl -X POST https://reqbin.com/echo/post/json-H 'Content-Type: application/json'-d '{"login":"ahed","password":"ahed"}'
what about -d? here we are mentioning the data to be sent to the server.
here is a good example from Microsoft documentation, used to generate a token based on the secret given.
$ curl -X POST https://directline.botframework.com/v3/directline/tokens/generate-H "Authorization: Bearer RCurR_XV9ZA.cwA.BKA.iaJrC8xpy8qbOF5xnR2vtCX7CZj0LdjAPGfiCpg4Fv0"
Yes, we can always interact with API using Curl Command Line Tool, HttpPie, Postman, or Rest client.
Last but not least
How about we send a form?
$ curl -X POST -H "Authorization : Bearer Token" -F 'file=@./file.txt" https://....
so the API requires you to send a file in a field labeled file.
@ = to specify the file location in our machine
Now What If we expect to receive back?
$ curl -X POST -H “Authorization : Bearer Token” -F ‘file=@./file.txt” https://.... -o filename.txt
- -o like output and we specify the name of the file that we will save the result followed by an extension.
Hope this was helpful 😄
Here is a really useful link for more practice