RESTful APIs are popular architectures for data transfer across the web.

RESTful APIs typically use HTTP, making them suitable for cases where statelessness is important.

Like any server-side language, you could interact with the HTTP protocol and make HTTP requests in Go.

laptop screen with an API testing app interface

This includes making HTTP requests, and you dont necessarily needexternal dependencies, like Gin or a database.

you’re free to use thehttppackage to consume APIs and fetch pages forweb scraping in Go.

Import these packages to start making HTTP requests in Go.

result from the GET request

In this tutorial, youll learn how to consume RESTful APIs using httpbins simple request and response service.

TheGetmethod takes in the URL, executes theGetrequest, and returns the response, including its headers and body.

you’ve got the option to handle any errors from the request depending on your requirements.

result from the POST request

If there are no errors, you might proceed to extract the information you need from theGetrequest.

The responsesBodyfield holds the body of the response.

Theelsestatement prints the response body to your console if there are no errors from the read operation.

Heres the result of theGETrequest to httpbins endpoint.

Heres a simple struct for encoding a JSON payload to the server as part of the POST request.

TheJSONstruct has theinfoandmessagestring fields, and youll initialize a struct instance for the request.

Theurlvariable stores the POST request endpoint from the httpbin website.

ThejsonInstancevariable is an instance of the JSON struct you might use to store and send structured data.

you’re able to use theMarshalmethod from thejsonpackage to format JSON for the request.

TheMarshalmethod also returns an error you might handle.

it’s possible for you to use thePostmethod to make POST requests.

ThePostmethod takes in the URL endpoint, the requests content throw in, and a buffer of the payload.

It returns the response and an error.

As thehttpbin documentationspecifies, this POST endpoint returns the request data that you send it.

Thehttppackage has the functions youll need for most of your operations.

There are many other packages in the standard library.