Maria đŠ Marshmallow
Posted on January 4, 2023
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:
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.
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:
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:)
You can also support me on Coinbase
Posted on January 4, 2023
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.