Curl Post Body



On this blog, we spend a lot of time pushing the limits of GraphQL and talking about some of the most advanced parts of the technology. However, GraphQL is inherently quite simple, and works just fine without any specialized tooling. So let’s demonstrate that by showing several very simple ways of calling a GraphQL API over HTTP.

  1. Curl Post Example
  2. Curl Post Body Parameters
  3. Curl Post Body

When submitting data via POST, you’ll usually include a body as well which both tools are capable of. You can see below using the -request parameter for curl and the Method parameter with Invoke-RestMethod, you can specify the verb. For the body, you’ll use the -data parameter in curl and the Body parameter with Invoke-RestMethod. Curl's -data will by default send Content-Type: application/x-www-form-urlencoded in the request header. However, when using Postman's raw body mode, Postman sends Content-Type: text/plain in the request header. So to achieve the same thing as Postman, specify -H 'Content-Type: text/plain' for curl.

The example API

For this post, we’ll rely on the simple todos API from apollographql/ac3-state-management-examples.

Let’s clone the repo, install the dependencies, and start the server locally.

If all goes well, you should see a message letting you know that your GraphQL server is up and running.

1. Using GraphQL IDEs (Apollo Explorer)

GraphQL IDEs let you build queries, browse schemas, and test out GraphQL APIs.

Apollo Explorer is a free cloud-based GraphQL IDE that comes with one-click query building, intelligent search, and a multitude of other productivity features to solve common pain-points we’ve heard from developers building apps with GraphQL.

Getting started should be faster than making a pot of coffee.

To use the explorer, we’ll head to studio.apollographql.com/dev and create an account (using either GitHub or your email).

Finally, choose a name for your graph, select the “Development” graph type, add your localhost endpoint (Apollo Server’s default is http://localhost:4000), and click “Create Graph”.

And that’s it! You’re ready to start querying.

Here’s a query you can use to get started. Paste this into the Operations window and click the blue button to run the query.

The Explorer is really useful! Not only is it easy to use, but we can see how long it took for the query to execute, download the responses, and keep track of all the operations you ran over time.

I don’t want to give you the impression that you need a specialized tool to access data from your GraphQL API. You don’t 😇. Let’s go way back to basics.

Curl post body from file

By the way, some other GraphQL IDEs you can check out are: GraphiQL, GraphQL Playground, and Insomnia.

2. Curl

curl is one of the most popular tools for accessing HTTP endpoints from the command line. It’s often used as a lowest-common-denominator example for how to access an API without any programming language or specialized tooling. So let’s use it to fetch GraphQL data!

Since curl is a command line tool, it’s easier to operate with queries that are just one line, so we’ll use the short one from above. Without further ado, here’s a curl command that will fetch a GraphQL query from our API:

It specifies four things about our request:

  1. ThePOSTHTTP verb, which is the most common one used by GraphQL APIs (although usually GET is also supported)
  2. The content type of application/json, because we’re specifying the query as part of a JSON object
  3. The data sent, which is a JSON document that looks like: { 'query': '{ todos { edges { node { id completed text } } } }' }. This JSON wrapper is helpful because you can also specify other options, like dynamic variables as part of the JSON object.
  4. The actual URL of the GraphQL endpoint, in this case http://locahost:4000
Curl Post Body

And that’s all! Using the above options, you can query nearly any GraphQL API, for example, Trevor Blades‘ countries API, which I love to use to introduce people to GraphQL for the first time:

Alright great, so we’ve found a completely vanilla way to send requests. For more details about exactly how GraphQL queries work, read Sashko Stubailo‘s post, “The Anatomy of a GraphQL Query.

3. Fetch

You can make a GraphQL HTTP request in literally any programming language, as long as you can set the above 4 parts correctly. So let’s convert that curl request into some JavaScript code with fetch, the new standard for getting HTTP results with promises. This will work in modern browsers with no libraries, but will require a polyfill in Node and some browsers. Let’s check out the code:

If you run this code Chrome’s DevTools, you’ll notice that the following output is printed to the console.

Neat, right? As you can see, the way we request GraphQL data using Fetch is pretty similar to the request we constructed with curl, but there are a few things here that would become pretty repetitive if we had to type them out every time. Not only that, but to effectively work with GraphQL data in most applications, you’ll likely want to cache it, display the data somehow, and re-render your view when it changes.

That sounds like something that a GraphQL client can help us with.

4. Using GraphQL clients (Apollo Client)

GraphQL client libraries are meant to help us work with more effectively build fully-fledged applications with GraphQL data.

Apollo Client handles the entire data-fetching and state management part of working with GraphQL data- we just need to hook it up and write the queries!

Curl Post Example

For example, if you’re building a React application, one of the most popular ways to query data using Apollo Client is to use the useQuery hook. Let’s demonstrate how we could fetch all the todos and display them in a list.

Passing the GraphQL query to the useQuery hook gives us an object that can be de-structured into loading, error, and data objects. If the query successfully executes and returns the GraphQL data from the server, we can map over the response data and display the elements in a list. Sweet!

Curl post body from file

To learn more about Apollo Client and how to set it up in your project, check out the getting started docs.

If you’re an iOS or Android developer, you can use the Apollo iOS and Apollo Android GraphQL clients which give you type-safe APIs to work with GraphQL servers.

Curl

Conclusion

Because GraphQL operates over HTTP, you can use any language, client library, or tool to fetch GraphQL data so long as you format the HTTP request properly. If you’re just getting started with GraphQL, I recommend using a tool like Apollo Explorer that’ll help you understand how to structure requests and explore your schema. And if you’re getting ready to build a web application that relies on GraphQL data, try out a GraphQL client library like Apollo Client; it saves you time by handling the mundane parts of app development, and lets you focus on coding the parts that makes your app special.

Curl Post Body Parameters

Special thanks to Sashko Stubailo who wrote the first version of this blog post nearly three years ago! Follow him on Twitter at @stubailo.

  • Joe R. & Teresa Lozano Long Campus
    7703 Floyd Curl Dr
    San Antonio, TX 78229

  • Greehey Academic & Research Campus
    8403 Floyd Curl Dr
    San Antonio, TX 78229
  • Regional Campus in Laredo
    1937 Bustamante St
    Laredo, TX 78041

Driving Directions

The main campus of UT Health San Antonio, 7703 Floyd Curl Drive, is in the South Texas Medical Center, and near Interstates 10 and 410. The Joe R. & Teresa Lozano Long Campus is 12 miles northwest of downtown San Antonio and 7 miles from San Antonio International Airport. The campus is bordered by Babcock Road, Merton Minter Boulevard and Floyd Curl, Louis Pasteur, and Medical Drive.

Curl

From Interstate 10
Exit at Medical Drive and turn west on Medical. (From I-10 West, this is to the left.) Take Medical for 1.5 miles to Floyd Curl Drive, which is the second stoplight past Fredericksburg Road. Turn left onto Floyd Curl. The main entrance is near Medical Drive. Ask at the gate for directions and parking.

Curl Post Body

From Interstate 410
Exit at Fredericksburg Road and turn north on Fredericksburg. Continue on Fredericksburg to Medical Drive. Turn left onto Medical. Turn left again onto Floyd Curl Drive, which is the second stoplight on Medical. The main entrance is on the right. Ask at the gate for directions and parking.