lostisland / faraday

Simple, but flexible HTTP client library, with support for multiple backends.
https://lostisland.github.io/faraday
MIT License
5.72k stars 972 forks source link

I am having trouble getting a JWT from a POST request. #1464

Closed oDinZu closed 1 year ago

oDinZu commented 1 year ago

Faraday Version: 2.7.1

Hi :wave: I am having trouble getting the JWT from my POST.

I receive a status code of 200 and the logger shows everything but the body.

The code:

    # build the connection to the API
    api_login = Faraday.new do |login|
      # add the class directly instead of using lookups
      login.use Faraday::Request::Json        # encode request params as json
      login.use Faraday::Request::UrlEncoded  # convert request params as "www-form-urlencoded"
      login.use Faraday::Response::RaiseError # raise an error if connection failed
      login.use Faraday::Response::Logger     # log the request to STDOUT

      # add by symbol, lookup from Faraday::Request
      # Faraday::Response and Faraday::Adapter registries
      login.response :json # decode response bodies as JSON

      #login.request :authorization, :basic, strapi_bot_user_email, strapi_bot_user_pass
      login.post(auth_login, { identifier: strapi_bot_user_email, password: strapi_bot_user_pass })
      login.adapter :httpx # must add adapter; default is Net:HTTP see README.md
    end

The STDOUT:

I, [2022-11-30T18:54:10.774765 #1]  INFO -- request: POST https://example.com/api/auth/local
I, [2022-11-30T18:54:10.774826 #1]  INFO -- request: Content-Type: "application/json"
I, [2022-11-30T18:54:11.047429 #1]  INFO -- response: Status 200
I, [2022-11-30T18:54:11.047551 #1]  INFO -- response: server: "nginx (Ubuntu)"
date: "Thu, 01 Dec 2022 00:54:11 GMT"
content-type: "application/json; charset=utf-8"
content-length: "345"
connection: "keep-alive"
...
 x-ratelimit-reset: "1669856102"
 x-powered-by: "ST"

I have tested many different variations.

I thought it would work with this: puts api_login.body; the puts api_login.headers works.

oDinZu commented 1 year ago

This populated the JWT: login.response :logger, nil, { bodies: true }

Edit: I am still unsure how to pass the debug log data.

INFO -- response: {"jwt":"exampleODYzNjQ1LCJleHAiOjE2NzI0NexamplepwTfbwEO8bZP4GNyPw","user":...

As an example, puts api_login.response("jwt")

oDinZu commented 1 year ago

I got the authorization token working, but things are very different when I need to authorize a user, then get that fresh user token to then generate json data from an API.

oDinZu commented 1 year ago

Hallelujah! :tada:

This code does exactly what I need it to do!

There is the first time this has been shared on the WWW for Ruby / Strapi integration. I hope it serves you well :heart: !

    connection = Faraday.new(auth_login) do |b|
      b.request :json # This will set the "Content-Type" header to application/json and call .to_json on the body
      b.response(:json, content_type: /\bjson$/)
      b.adapter :httpx # default is Net:HTTP see README.md
    end

    response = connection.post(auth_login, { "identifier": strapi_bot_user_email, "password": strapi_bot_user_pass })
    strapi_token = response.body["jwt"]