Scala:http4s 对于在curl/requests 中工作的相同请求给出 401 Unauthorized

标签 scala curl http4s cats-effect http4s-circe

我使用http4s v0.19.0尝试了以下代码:

import cats.effect._

def usingHttp4s(uri: String, bearerToken: String)(implicit cs: ContextShift[IO]): String = {
    import scala.concurrent.ExecutionContext
    import org.http4s.client.dsl.io._
    import org.http4s.headers._
    import org.http4s.Method._
    import org.http4s._
    import org.http4s.client._


    import org.http4s.client.middleware._

    val blockingEC = ExecutionContext.fromExecutorService(Executors.newFixedThreadPool(5))

    val middlewares = Seq(
      RequestLogger[IO](logHeaders = true, logBody = true, redactHeadersWhen = _ => false)(_),
      FollowRedirect[IO](maxRedirects = 5)(_)
    )

    val client = middlewares.foldRight(JavaNetClientBuilder(blockingEC).create[IO])(_.apply(_))

    val req = GET(
      Uri.unsafeFromString(uri),
      Authorization(Credentials.Token(AuthScheme.Bearer, bearerToken))
    )
    client.expect[String](req).unsafeRunSync()
  }

我收到以下错误:

[error] (run-main-0) org.http4s.client.UnexpectedStatus: unexpected HTTP status: 401 Unauthorized
[error] org.http4s.client.UnexpectedStatus: unexpected HTTP status: 401 Unauthorized

不仅如此,我的程序从未退出(我是否必须关闭某些客户端!?),即使我连接了日志中间件,它也从未打印请求

我接下来尝试了@li-haoyi的request库,这没有返回错误:

def usingLiHaoyiRequest(uri: String, bearerToken: String): String =
    requests.get(uri, headers = Iterable("Authorization" -> s"Bearer $bearerToken")).text()

上面的代码适用于相同的uri和相同的baseToken,所以我的 token 不可能是错误的。为了更加确定,我尝试了curl:

curl -L -H "Authorization: Bearer ${BEARER}" ${URI}

此问题也会发生在 http4s v0.18.19 中(即使使用显式 Json 和接受 header ):

import io.circle.Json

def usingHttp4s(uri: String, bearerToken: String): Json = {
    import org.http4s.client.dsl.io._
    import org.http4s.headers._
    import org.http4s.Method._
    import org.http4s._
    import org.http4s.client.blaze.Http1Client
    import org.http4s.client.middleware._
    import org.http4s.circe._
    import org.http4s.MediaType._

    val program = for {
      c <- Http1Client[IO]()
      client = FollowRedirect(maxRedirects = 5)(c)
      req = GET(
        Uri.unsafeFromString(uri),
        Authorization(Credentials.Token(AuthScheme.Bearer, bearerToken)),
        Accept(`application/json`)
      )
      res <- client.expect[Json](req)
    } yield res

    program.unsafeRunSync()
  }

所以我的问题是:

  1. 为什么 requestscurl 都能工作,但 http4s 却给了我 401 相同的请求?
  2. 为什么我的 http4s 版本永远不会退出?
  3. 为什么请求记录器中间件不记录请求?

最佳答案

gitter room 中所述,该错误存在于 http4s 中,它不会将授权 header 转发到重定向,但curl 和 requests 都会转发(只要转发到同一子域)。

关于Scala:http4s 对于在curl/requests 中工作的相同请求给出 401 Unauthorized,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53783653/

相关文章:

scala - 在 Scala 中使用 listFiles 只获取文件名

scala - 使用 Applicative Functor 对选项列表求和

scala - 用 scala 替换列表中的元素

scala - 如何在 Scala 中使用 Tapir 创建具有多个模式的端点

scala - 编译包含 java 和 scala 代码的文件

c++ - 将 cURL 库加载到 Qt5 后出现错误

python - Squid 代理在 python 发出的 GET 请求上给出 501 但不 curl

python - 将curl转换为python : usernames and passwords

scala - 在 Http4s API 中将 `Option[A]` 转换为 Ok() 或 NotFound()

fs2 - http4s 收到过早的 EOF