Coder Social home page Coder Social logo

zorbash / req Goto Github PK

View Code? Open in Web Editor NEW

This project forked from wojtekmach/req

0.0 1.0 0.0 121 KB

Req is an HTTP client with a focus on ease of use and composability, built on top of Finch.

Home Page: https://hexdocs.pm/req

Elixir 100.00%

req's Introduction

Req

CI

Docs

Req is an HTTP client with a focus on ease of use and composability, built on top of Finch.

Features

  • Extensibility via request, response, and error steps

  • Automatic body decompression (via decompress step)

  • Automatic body encoding and decoding (via encode_body and decode_body steps)

  • Encode params as query string (via put_params step)

  • Basic, bearer and .netrc authentication (via auth step)

  • Range requests (via put_range step)

  • Follows redirects (via follow_redirects step)

  • Retries on errors (via retry step)

  • Basic HTTP caching (via cache step)

  • Setting base URL (via put_base_url step)

Usage

The easiest way to use Req is with Mix.install/2 (requires Elixir v1.12+):

Mix.install([
  {:req, "~> 0.2.0"}
])

Req.get!("https://api.github.com/repos/elixir-lang/elixir").body["description"]
#=> "Elixir is a dynamic, functional language designed for building scalable and maintainable applications"

If you want to use Req in a Mix project, you can add the above dependency to your mix.exs.

Example POST request:

Req.post!("https://httpbin.org/post", {:form, comments: "hello!"}).body["form"]
#=> %{"comments" => "hello!"}

Low-level API

Under the hood, Req works by passing a %Req.Request{} struct through a series of steps.

Request steps are used to refine the data that will be sent to the server.

After making the actual HTTP request, we'll either get a HTTP response or an error. The request, along with the response or error, will go through response or error steps, respectively.

Nothing is actually executed until we run the pipeline with Req.Request.run/1.

The high-level API shown before:

Req.get!("https://api.github.com/repos/elixir-lang/elixir")

is equivalent to this composition of lower-level API functions and steps:

Req.Request.build(:get, "https://api.github.com/repos/elixir-lang/elixir")
|> Req.Steps.put_default_steps()
|> Req.Request.run!()

(See Req.Request.build/3, Req.Steps.put_default_steps/2, and Req.Request.run!/1 for more information.)

We can also build more complex flows like returning a response from a request step or an error from a response step. See Req.Request documentation for more information.

Acknowledgments

Req is built on top of Finch and is inspired by cURL, Requests, Tesla, and many other HTTP clients - thank you!

License

Copyright (c) 2021 Wojtek Mach

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

req's People

Contributors

wojtekmach avatar mhanberg avatar srowley avatar a8t avatar jonatanklosko avatar heydtn avatar princemaple avatar quinnwilton avatar thbar avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.