In this article, we’re going to discuss how to use curl
to interact with RESTful APIs. curl
is a command-line utility that can be used to send requests to an API.
API requests are made up of four different parts:
- The endpoint. This is the URL which we send requests to.
- The HTTP method. The action we want to perform. The most common methods are
GET
POST
PUT
DELETE
andPATCH
- The headers. The headers which we want to send along with our request, e.g. authorization header.
- The body. The data we want to send to the api.
curl Syntax
To Curly Girl experts, the Curly Girl Method involves understanding your hair’s porosity level, feeding your strands the correct amount of protein, and remembering a long list of sulphates and silicones to avoid like the plague. Curls come in all colors, Shop Curly, Your online shopping resource for the best products for curls waves and kinky afro styling in Boston and now serving the Netherlands! Celebrating healthy and happy coils, waves, zigzags, spirals and bouncy hair. In vector calculus, the curl is a vector operator that describes the infinitesimal circulation of a vector field in three-dimensional Euclidean space. The curl at a point in the field is represented by a vector whose length and direction denote the magnitude and axis of the maximum circulation.
The syntax for the curl
command is:
The options we will cover in this post are:
-X
or--request
- HTTP method to be used-i
or--include
- Include the response headers-d
or--data
- The data to be sent to the API-H
or--header
- Any additional headers to be sent
HTTP GET
The GET method is used to fetch a resource from a server. In curl
, the GET method is the default method, so we don’t need to specify it.
Example:
GET With Query Parameters
We can also send query parameters along with the curl
GET request.
Example:
HTTP POST
The POST method is used to create a resource on the server.
To send a curl
POST request we use the option -X POST
.
POST Form Data
Example:
By default, curl
uses Content-Type: application/x-www-form-urlencoded
as the Content-Type
header, so we don’t need to specify it when sending form data.
POST JSON
To POST a JSON by curl
we have to specify the Content-Type
as application/json
.
Example:
HTTP PUT
The PUT method is used to update or replace a resource on the server. It replaces all data of the specified resource with the supplied request data.
To send a curl
PUT request we use the option -X PUT
.
Example:
The above PUT request will replace our previously created post with “New post title” and “New post body”.
HTTP PATCH
The PATCH method is used to make partial updates to the resource on the server.
To send a curl
PATCH request we use the option -X PATCH
.
Curl Method Post
Example:
Notice how we are only sending the body with “Updated post content” as we are doing a partial update.
HTTP DELETE
The DELETE method is used to remove the specified resource from the server.
To send a curl
DELETE request we use the option -X DELETE
.
Authentication
Sometimes an API endpoint has restricted access and will only serve requests to authenticated and authorized users. For these requests, we have to provide an access token in the header of the request.
To send a curl
header, we use: -H
option.
The following request sends POST request with a bearer token in the header:
Conclusion
In this post we learned how to send HTTP requests (GET, POST, PUT, PATCH and DELETE) to an API using curl commands.
Show Mobile NoticeShow All NotesHide All NotesSection 6-1 : Curl and Divergence
Before we can get into surface integrals we need to get some introductory material out of the way. That is the purpose of the first two sections of this chapter.
In this section we are going to introduce the concepts of the curl and the divergence of a vector.
Let’s start with the curl. Given the vector field (vec F = P,vec i + Q,vec j + R,vec k) the curl is defined to be,
There is another (potentially) easier definition of the curl of a vector field. To use it we will first need to define the (nabla ) operator. This is defined to be,
We use this as if it’s a function in the following manner.
[nabla f = frac{{partial f}}{{partial x}},vec i + frac{{partial f}}{{partial y}},vec j + frac{{partial f}}{{partial z}},vec k]So, whatever function is listed after the (nabla ) is substituted into the partial derivatives. Note as well that when we look at it in this light we simply get the gradient vector.
Using the (nabla ) we can define the curl as the following cross product,
We have a couple of nice facts that use the curl of a vector field.
Facts
- If (fleft( {x,y,z} right)) has continuous second order partial derivatives then ({mathop{rm curl}nolimits} left( {nabla f} right) = vec 0). This is easy enough to check by plugging into the definition of the derivative so we’ll leave it to you to check.
- If (vec F) is a conservative vector field then ({mathop{rm curl}nolimits} vec F = vec 0). This is a direct result of what it means to be a conservative vector field and the previous fact.
- If (vec F) is defined on all of ({mathbb{R}^3}) whose components have continuous first order partial derivative and ({mathop{rm curl}nolimits} vec F = vec 0) then (vec F) is a conservative vector field. This is not so easy to verify and so we won’t try.
So, all that we need to do is compute the curl and see if we get the zero vector or not.
[begin{align*}{mathop{rm curl}nolimits} vec F & = left| {begin{array}{*{20}{c}}{vec i}&{vec j}&{vec k}{displaystyle frac{partial }{{partial x}}}&{displaystyle frac{partial }{{partial y}}}&{displaystyle frac{partial }{{partial z}}}{{x^2}y}&{xyz}&{ - {x^2}{y^2}}end{array}} right| & = - 2{x^2}y,vec i + yz,vec k - left( { - 2x{y^2},vec j} right) - xy,vec i - {x^2}vec k & = - left( {2{x^2}y + xy} right)vec i + 2x{y^2},vec j + left( {yz - {x^2}} right)vec k & ne vec 0end{align*}]So, the curl isn’t the zero vector and so this vector field is not conservative.
Next, we should talk about a physical interpretation of the curl. Suppose that (vec F) is the velocity field of a flowing fluid. Then ({mathop{rm curl}nolimits} vec F) represents the tendency of particles at the point (left( {x,y,z} right)) to rotate about the axis that points in the direction of ({mathop{rm curl}nolimits} vec F). If ({mathop{rm curl}nolimits} vec F = vec 0) then the fluid is called irrotational.
Let’s now talk about the second new concept in this section. Given the vector field (vec F = P,vec i + Q,vec j + R,vec k) the divergence is defined to be,
There is also a definition of the divergence in terms of the (nabla ) operator. The divergence can be defined in terms of the following dot product.
There really isn’t much to do here other than compute the divergence.
[{mathop{rm div}nolimits} vec F = frac{partial }{{partial x}}left( {{x^2}y} right) + frac{partial }{{partial y}}left( {xyz} right) + frac{partial }{{partial z}}left( { - {x^2}{y^2}} right) = 2xy + xz]We also have the following fact about the relationship between the curl and the divergence.
[{mathop{rm div}nolimits} left( {{mathop{rm curl}nolimits} vec F} right) = 0] Example 3 Verify the above fact for the vector field (vec F = y{z^2},vec i + xy,vec j + yz,vec k). Show SolutionLet’s first compute the curl.
[begin{align*}{mathop{rm curl}nolimits} vec F & = left| {begin{array}{*{20}{c}}{vec i}&{vec j}&{vec k}{displaystyle frac{partial }{{partial x}}}&{displaystyle frac{partial }{{partial y}}}&{displaystyle frac{partial }{{partial z}}}{y{z^2}}&{xy}&{yz}end{array}} right| & = z,vec i + 2yz,vec j + y,vec k - {z^2}vec k & = zvec i + 2yz,vec j + left( {y - {z^2}} right)vec kend{align*}]Now compute the divergence of this.
[{mathop{rm div}nolimits} left( {{mathop{rm curl}nolimits} vec F} right) = frac{partial }{{partial x}}left( z right) + frac{partial }{{partial y}}left( {2yz} right) + frac{partial }{{partial z}}left( {y - {z^2}} right) = 2z - 2z = 0]We also have a physical interpretation of the divergence. If we again think of (vec F) as the velocity field of a flowing fluid then ({mathop{rm div}nolimits} vec F) represents the net rate of change of the mass of the fluid flowing from the point (left( {x,y,z} right)) per unit volume. This can also be thought of as the tendency of a fluid to diverge from a point. If ({mathop{rm div}nolimits} vec F = 0) then the (vec F) is called incompressible.
The next topic that we want to briefly mention is the
Curly Girl Method Approved Products
[{mathop{rm div}nolimits} left( {nabla f} right) = nabla centerdot nabla f = {f_{xx}} + {f_{yy}} + {f_{zz}}]The Laplace operator is then defined as,
[{nabla ^2} = nabla centerdot nabla ]The Laplace operator arises naturally in many fields including heat transfer and fluid flow.
The final topic in this section is to give two vector forms of Green’s Theorem. The first form uses the curl of the vector field and is,
where (vec k) is the standard unit vector in the positive (z) direction.
The second form uses the divergence. In this case we also need the outward unit normal to the curve (C). If the curve is parameterized by
Curl Method Post
[vec rleft( t right) = xleft( t right)vec i + yleft( t right)vec j]Curl Method Delete
then the outward unit normal is given by,
[vec n = frac{{y'left( t right)}}{{left| {vec r'left( t right)} right|}}vec i - frac{{x'left( t right)}}{{left| {vec r'left( t right)} right|}}vec j]Here is a sketch illustrating the outward unit normal for some curve (C) at various points.
Curl Method Head
The vector form of Green’s Theorem that uses the divergence is given by,