Curl To Requests



  1. Curl will send form urlencoded by default, so for json the Content-Type header must be explicitly set; This gist provides examples for using both formats, including how to use sample data files in either format with your curl requests. For sending data with POST and PUT requests, these are common curl options: request type-X POST-X PUT.
  2. What curl actually does is let you interact with remote systems by making requests to those systems, and retrieving and displaying their responses to you. Those responses might well be web page content and files, but they can also contain data provided via a web service or API as a result of the “question” asked by the curl request.
  3. Curl is a command line tool to transfer data to or from a server, using any of the supported protocols (HTTP, FTP, IMAP, POP3, SCP, SFTP, SMTP, TFTP, TELNET, LDAP or FILE). Curl is powered by Libcurl.
  1. Curl Requests In Postman
  2. Curl Requests To Json
  3. Curl To Requests
  4. Curl Requests Windows
  5. Curl Requests Javascript

(P)Bookmarks.dev - Open source Bookmarks and Code Snippets Manager for Developers & Co. See our How To guides to help you get started. Public Bookmarks Repo on Github -

Curltorequests curltorequests is a Python module that converts cURL commands into equivalent Python code using the requests library. Many cURL features are unsupported (it's a very complex piece of software), but curltorequests is far more complete than any of the other cURL converting utilities I've found.

If you want to quickly test your REST api from the command line, you can use curl. In this post I will present how to execute GET, POST, PUT, HEAD, DELETE HTTP Requests against a REST API. For the purpose of this blog post I will be using the REST api that supports www.bookmarks.dev. The API is documented with OpenAPI and available for testing in the browser at https://www.bookmarks.dev/api/docs/.

  • Introduction
  • CRUD Operations on Bookmarks.dev API

Introduction

In the first part of the blog post I will do a brief introduction to curl and what it can do (HTTP requests with options). In the second part I will show examples with different HTTP operations from bookmarks.dev-api.

What is curl?

Curl is a command line tool and library for transferring data with URL syntax, supporting DICT, FILE, FTP, FTPS, Gopher, HTTP, HTTPS, IMAP, IMAPS, LDAP, LDAPS, POP3, POP3S, RTMP, RTSP, SCP, SFTP, SMTP, SMTPS, Telnet and TFTP. curl supports SSL certificates, HTTP POST, HTTP PUT, FTP uploading, HTTP form based upload, proxies, HTTP/2, cookies, user+password authentication (Basic, Digest, NTLM, Negotiate, Kerberos…), file transfer resume, proxy tunneling and more.

As mentioned, I will be using curl to simulate HEAD, GET, POST, PUT and DELETE request calls against a REST API.

HEAD requests

If you want to check if a resource is serviceable, what kind of headers it provides and other useful meta-information written in response headers, without having to transport the entire content, you can make a HEAD request.

Let’s say I want to see what I would GET when requesting latest public bookmarks. I would issue the following HEAD request with curl:

Request

OR

Curl options

  • -I or --head - fetch the headers only
  • -i, --include - include the HTTP response headers in the output
  • -X, --request - specify a custom request method to use when communicating with the HTTP server (GET, PUT, DELETE&)

Response

Note the following headers

  • Access-Control-Allow-Headers: Content-Type, Authorization, Location
  • Access-Control-Allow-Methods: POST, GET, PUT, PATCH, DELETE, OPTIONS
  • Access-Control-Allow-Origin: *

in the response.

They’ve been added to support Cross-Origing Resource Sharing (CORS).

GET request

Executing curl with no parameters on a URL (resource) will execute a GET.

Request

Curl to requests

Curl Requests In Postman

which is equivalent with

Response

Note the use of accept: application/json

Curl options

  • -H, --header : customer header to pass to the server

If you want to have it displayed prettier I suggest you use a tool like jq:

Request

Response

If you don’t want the progress meter (first part) shown, you can silent curl:

Request

Response

Curl options

  • -s, --silent : silent or quiet mode. Don’t show progress meter or error messages. Makes Curl mute. It will still output the data you ask for, potentially even to the terminal/stdout unless you redirect it.

Curl Requests To Json

An alternative to jq is to use Python if you have it on your machine:

Curl request with multiple headers

All the responses from the api of bookmarks.dev are gzipped.We could ask for the gzipped variant by issuing the following request:

Request

Curl options

  • -v, --verbose : the opposite of --silent, make the operation more talkative

To achieve that you need to simply add another-H option with the corresponding value. In this case you would get some unreadable characters in the content, if you do not redirect the response to a file:

Response

CRUD Operations on Bookmarks.dev API

Those were some basic curl HTTP calls with a few options. Now we will combine them and show examples against a productionready API. For the examples I will use the API running on localhost. It is really easy to setup with Docker-compose ifyou follow the instructions from the Readme file.

The API is protected with Keycloak and bearer token. A way to obtain a bearer token in Keycloak is to enable Direct Access Grants for the client - this corresponds to the Resource Owner Password Credentialsin the OAuth2 Specification. Thus the user’s credentials are sent within form parameters. Of course we can do that with curl too:

Request

The the username and password are from the initial set up.

The response looks something like the following:

With jq is really easy to extract just the access_token:

Request

Curl To Requests

Response

Curl options

  • -d, --data : (HTTP) Sends the specified data in a POST request to the HTTP server, in the same way that a browser does when a user has filled in an HTML form and presses the submit button. This will cause curl to pass the data to the server using the content-type application/x-www-form-urlencoded.

Create bookmark - POST

Curl

Request

Note the Bearer token is reduced here (Bearer eyJhbGciOiJ....) and in the following examples for brevity

Response

Note the location header - it contains the URL of the new created resource

Read created resource - GET

We will read the previously created bookmark by issuing an GET request on the url from the location header

Curl Requests Windows

Response

Update created resource - PUT

Request

Response

Curl Requests Javascript

Delete created resource - DELETE

Request

Response

Note the 204 OK Status showing that everything worked as expected.

Trying to execute the deletion again will result in an 404 resource NOT_FOUND status:

Response

The stacktrace is shown because we are in dev modus

Conclusion

I am just scratching the surface in this blog post. Check out the curl docs for furthercapabilities.