DEV Community

Cover image for What is cURL and why is it important in API testing?πŸ€–
Maria 🍦 Marshmallow
Maria 🍦 Marshmallow

Posted on • Updated on

 

What is cURL and why is it important in API testing?πŸ€–

Today’s post is devoted to one of the inevitable parts of API testing – cURL.

cURL (client URL) – is a command line tool and library for transferring data with URLs.

Developers use cURL to send and receive data to and from servers. Specifically, any user can specify a server URL (the location to which they want to submit a request) and the data they want to send to that server URL using this command line interface (CLI). cURL supports multiple protocols (schemes), like DICT, FILE, FTP(S), GOPHER(S), IMAP(S), MQTT, RTSP, etc. and of course HTTP.

Depending on the protocol used for cURL, the syntax may change. Here we will focus on the HTTP protocol cURL structure as it is most commonly used in API testing. The standard cURL structure will look like this:

curl - [method] URL [options]

Place of cURL in API testing

In API testing, file transfers or a series of similar actions can be automated using cURL. It is a useful tool, for instance, for modeling client actions using an API. Basically, by using cURL, users can import or export different requests with all necessary parameters, like query parameters, headers, cookies, auth, etc., to any app or platform they need, spending little time.

Let’s take a look at some examples.

Using cURL command in API Tester app

Imagine that you have some GET request which you would like to send to the server, how can you import it into to the API Tester app? There are two main options.
First, you can do it manually, find the source, copy the request URL from there, open the app, create a new request, and then paste it into the URL field.
Second, you copy the cURL of the request you would like to send and paste it directly to the app, while the app itself defines what request type you need and paste all the info from the cURL into the correct request fields. What do you think will take less time and require less effort? To my mind, the answer is obvious.

That’s how a simple GET request’s cURL may look like:

curl -X GET β€˜https://httpbin.org/json'

As you can see, cURL contains the request method, URL, and no additional params. In the API Tester app, this will be displayed as follows:

Image description

See, how easy that was? All you need is just to copy cURL and paste it to the app. That’s how cURL works in API testing.

Now, let’s take a look at a more complex request with different parameters. As an example, I took a PATCH request from the Zoom API:

curl -X PATCH 'https://api.zoom.us/v2/videosdk/sessions/KkMHZ4y8QhSUWAHi5sWvfg==/livestream' -H 'Content-Type:application/json' -d β€˜{"stream_url":"https://example.com/livestream","stream_key":"ABCDEFG12345HIJ6789","page_url":"https://example.com/livestream/123"}'

This cURL contains info about request method (PATCH), header (H β€˜Content-Type:application/json’), and body (d β€˜{Β«stream_urlΒ»:Β»https://example.com/livestream","stream_key":"ABCDEFG12345HIJ6789","page_url":"https://example.com/livestream/123"}).

On the screenshots below, you can see that all request data was successfully transferred to the app.

Image description

What is great about cURL is that you can transfer large amounts of data. In the next example, we will try to import a GraphQL request with a query and variables:

curl -X POST 'https://graphqlpokemon.favware.tech/' -H 'Content-Type:application/json' -d '{"query":"fragment data on Pokemon {\n num\n species\n types\n abilities { first second hidden }\n baseStats { hp attack defense specialattack specialdefense speed }\n gender { male female }\n height\n weight\n flavorTexts { game flavor }\n evYields { hp attack defense specialattack specialdefense speed }\n isEggObtainable\n minimumHatchTime\n maximumHatchTime\n levellingRate\n sprite\n shinySprite\n backSprite\n shinyBackSprite\n smogonTier\n smogonPage\n serebiiPage\n bulbapediaPage\n}\n\nquery($pokemon: PokemonEnum! $reverse: Boolean! $take: Int!) {\n getPokemon(pokemon: $pokemon reverseFlavorTexts: $reverse takeFlavorTexts: $take) {\n ...data\n }\n}Β»,"variables":{"pokemon":"arceus","reverse":true,"take":1}}'

Here we can see that besides the standard request method, URL, header query and variables are listed as well. After importing this cURL in API Tester, we will see that all request parts are transferred correctly:

Image description

How to create cURL?

We discussed how to import different cURLs into the app, but we used a cURL that has already been created. The question is, how were they created? Each cURL is generated by someone using different apps or websites – this is as easy to do as importing a cURL that has already been created.

In API Tester, you can generate cURL as well as import them. To create a cURL, you need to open any request, add additional parameters (if required), tap on the three dots icon on the upper part of the request screen, there you will see β€˜Export cURL’ - tap it and your cURL is done.

The cURL we generated looks like this:

curl -X POST 'https://petstore.swagger.io/v2/pet' -H 'Content-Type:application/json' -d '{"type":"object","required":["name","photoUrls"],"properties":{"id":{"type":"integer","format":"int64"},"category":{"type":"object","properties":{"id":{"type":"integer","format":"int64"},"name":{"type":"string"}},"xml":{"name":"Category"}},"name":{"type":"string","example":"doggie"},"photoUrls":{"type":"array","xml":{"wrapped":true},"items":{"type":"string","xml":{"name":"photoUrl"}}},"tags":{"type":"array","xml":{"wrapped":true},"items":{"xml":{"name":"tag"},"type":"object","properties":{"id":{"type":"integer","format":"int64"},"name":{"type":"string"}}}},"status":{"type":"string","description":"pet status in the storeΒ»,"enum":["available","pending","sold"]}},"xml":{"name":"Pet"}}'

You can copy it and share with someone, or paste it into the app again, as we did at the beginning of the post.

Conclusion

cURL is an extremely useful tool for any person who tests API. It not only saves a lot of time, but also allows avoid mistakes when transferring data between different sources. Of course, in this post I just showed some basics which every user must know, and the cURL command line is much more complicated.

Thanks for reading! I hope you found this article helpful. Feel free to leave any questions, comments, or suggestions.

Btw, you can support my work by buying me a coffee! I'll leave here a few links for you:)

Buy Me a Coffee at ko-fi.com

You can also support me on Coinbase

Top comments (24)

Collapse
 
jonrandy profile image
Jon Randy πŸŽ–οΈ

Another tip - you can get the equivalent cURL command for any request your browser makes by selecting 'Copy > Copy as cURL' from the context menu of any request in the network tab

Collapse
 
mariamarsh profile image
Maria 🍦 Marshmallow

Thanks for the advice, I believe it will help someone 😍

Collapse
 
mjvmroz profile image
Michael Mroz • Edited

I know some people who still use curl, but most have gravitated to higher-level tools.

I'm personally a fan of the HTTPie CLI for anything ad hoc, which is much less verbose than curl for most tasks. Every now and then I use curl directly, but it's very rare.

The GUI space is more saturated and mostly geared around reusable definitions and chaining requests to build workflows. Postman is probably the most established player there, but Swagger, Insomnia and plenty others exist. The makers of HTTPie recently launched their own too, resulting in the original CLI tool being rebranded "HTTPie for Terminal".

Collapse
 
mariamarsh profile image
Maria 🍦 Marshmallow

It all depends on the preferences of each of us πŸ™ƒ
By the way, API Tester mobile app supports import from Postman and Swagger that you mentioned 😏

Collapse
 
lucassperez profile image
Lucas Perez

I've been using curl for so long, but somehow I have never associated its name with URL wtf, even having read the manual. πŸ˜‚

Love it, great tool!

Collapse
 
mariamarsh profile image
Maria 🍦 Marshmallow

Haha it's funny 😝
Thank you Lucas❀️

Collapse
 
itsahsanmangal profile image
Ahsan Mangal {Frontend Developer} πŸ‘¨πŸ»β€πŸ’»βœ¨πŸ©β˜•οΈπŸ’

cURL is a command-line tool that allows you to transfer data from or to a server. It supports various protocols, including HTTP, HTTPS, FTP, and more. cURL is often used to troubleshoot network issues, test APIs, and automate tasks.

Collapse
 
mariamarsh profile image
Maria 🍦 Marshmallow

Thanks for your interest and clarification😊

Collapse
 
igibsonconor profile image
Conor Gibson

Thanks for the article, it's a great explanation.

Collapse
 
mariamarsh profile image
Maria 🍦 Marshmallow

It's my pleasure, thank you Conor😘

Collapse
 
leob profile image
leob

Yes!

Curl, I've always loved it as a simple command line tool to do some quick API testing :)

Collapse
 
mariamarsh profile image
Maria 🍦 Marshmallow

Take a look at the API Tester mobile app at your leisure. I think you will appreciate its use when there is no laptop at hand at the right time.πŸ˜‰

Collapse
 
alessioferrine profile image
alessioferrine

Import is a quite simple but really useful thing πŸ‘
Thanks for writing about it!

Collapse
 
mariamarsh profile image
Maria 🍦 Marshmallow

Nice to hear that 😌

Collapse
 
classx profile image
classx
Collapse
 
mariamarsh profile image
Maria 🍦 Marshmallow

Thanks for the recommendation πŸ™Œ
This is also an interesting tool, I will explore it in more detail and make a personal comparison with cURL. If I'll dind it fascinating, maybe I will write a post about it πŸ€”

Collapse
 
magicwalter15 profile image
magicwalter15

I will definitely try this, It looks very useful.

Collapse
 
mariamarsh profile image
Maria 🍦 Marshmallow

I'm glad to helpπŸ’–
Let me know how it goes.

Collapse
 
liviufromendtest profile image
Liviu Lupei

Just dropping a shameless plug here.
You can also use Endtest to perform API testing.

Documentation for that:
app.endtest.io/guides/docs/send-ap...

Collapse
 
mariamarsh profile image
Maria 🍦 Marshmallow

Thanks for the suggestion, I'll explore it and form my own opinionπŸ€“

Collapse
 
mariamarsh profile image
Maria 🍦 Marshmallow

As for this, I can recommend this post about top tools, maybe someone will find something useful 😌
Thunder Client is also there 😏

Some comments have been hidden by the post's author - find out more

Advice For Junior Developers

Advice from a career of 15+ years for new and beginner developers just getting started on their journey.