What is cURL and why is it important in API testing?

Read Time:5 Minute, 0 Second

Todays 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]

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.

Lets take a look at some examples.

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.

Thats how a simple GET requests 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. Thats how cURL works in API testing.

Now, lets 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 numn speciesn typesn abilities { first second hidden }n baseStats { hp attack defense specialattack specialdefense speed }n gender { male female }n heightn weightn flavorTexts { game flavor }n evYields { hp attack defense specialattack specialdefense speed }n isEggObtainablen minimumHatchTimen maximumHatchTimen levellingRaten spriten shinySpriten backSpriten shinyBackSpriten smogonTiern smogonPagen serebiiPagen bulbapediaPagen}nnquery($pokemon: PokemonEnum! $reverse: Boolean! $take: Int!) {n getPokemon(pokemon: $pokemon reverseFlavorTexts: $reverse takeFlavorTexts: $take) {n …datan }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

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.

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.

Source: https://dev.to/mariamarsh/what-is-curl-and-why-is-it-important-in-api-testing-416m

WP Ad Inserter plugin for WordPress