Basic Auth不使用http-builder-ng - ng?
不传递授权请求build.gradle
group 'org.vinay.rest'
version '1.0-SNAPSHOT'
apply plugin: 'groovy'
apply plugin: 'java'
sourceCompatibility = 1.8
repositories {
mavenCentral()
jcenter()
}
dependencies {
// https://mvnrepository.com/artifact/io.github.http-builder-ng/http-builder-ng-core
testRuntime group: 'io.github.http-builder-ng', name: 'http-builder-ng-core', version: '0.17.0'
testCompile group: 'junit', name: 'junit', version: '4.12'
testCompile group: 'org.spockframework', name: 'spock-core', version: '1.0-groovy-2.4'
testCompile group: 'ch.qos.logback', name: 'logback-classic', version:'1.0.13'
}
GetD2LUserBySourcedIdSpec.groovy
import spock.lang.Specification
import groovyx.net.http.HttpBuilder
/**
* Created by e5017581 on 11/07/2017.
*/
class GetD2LUserBySourcedIdSpec extends Specification {
def "test get /users call with d2lSourced Id"() {
given:
def httpClient = HttpBuilder.configure {
request.uri = 'http://esbvucmv5.vu.edu.au/D2L/2.0'
//request.headers['Authorization'] = 'Basic ZDJscnM6MTRkMmxycw=='
//request.headers['Content-Type'] = "application/json"
request.auth.basic'd2lrs', '14d2lrs'
}
when:
def user = httpClient.get {
//request.auth.basic 'd2lrs', '14d2lrs'
//request.headers['Authorization'] = 'Basic ZDJscnM6MTRkMmxycw=='
request.uri.path = '/users/'
request.uri.query = [sourcedId: 's4504565']
}
then:
println user.dump()
null != user
}
}
HTTP的日志:
com.intellij.rt.execution.junit.JUnitStarter -ideVersion5 "GetD2LUserBySourcedIdSpec,test get /users call with d2lSourced Id"
15:41:36,714 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Found resource [logback.groovy] at [file:/C:/Users/e5017581/IdeaProjects/http-builder-ng2/build/resources/test/logback.groovy]
15:41:36,935 |-INFO in ch.qos.logback.classic.gaffer.ConfigurationDelegate@6d07a63d - Added status listener of type [ch.qos.logback.core.status.OnConsoleStatusListener]
15:41:36,946 |-INFO in ch.qos.logback.classic.gaffer.ConfigurationDelegate@6d07a63d - About to instantiate appender of type [ch.qos.logback.core.ConsoleAppender]
15:41:36,947 |-INFO in ch.qos.logback.classic.gaffer.ConfigurationDelegate@6d07a63d - Naming appender as [CONSOLE]
15:41:37.100 [main] DEBUG g.net.http.JavaHttpBuilder.headers - Request-Header: Accept-Encoding -> [gzip, deflate]
15:41:37.306 [main] DEBUG g.net.http.JavaHttpBuilder.content - Response-Body: null
15:41:37.307 [main] DEBUG g.net.http.JavaHttpBuilder.headers - Response-Header: Date -> Tue, 11 Jul 2017 05:41:37 GMT
15:41:37.307 [main] DEBUG g.net.http.JavaHttpBuilder.headers - Response-Header: Content-Type -> text/plain
15:41:37.307 [main] DEBUG g.net.http.JavaHttpBuilder.headers - Response-Header: Content-Length -> 42
15:41:37.307 [main] DEBUG g.net.http.JavaHttpBuilder.headers - Response-Header: Connection -> keep-alive
15:41:37.307 [main] DEBUG g.net.http.JavaHttpBuilder.headers - Response-Header: Server -> Mule/SNAPSHOT
15:41:37.307 [main] DEBUG g.net.http.JavaHttpBuilder.headers - Response-Header: WWW-Authenticate -> Basic realm="realm"
15:41:37.307 [main] DEBUG g.net.http.JavaHttpBuilder.headers - Response-Header: http.status -> 401
15:41:37.307 [main] DEBUG g.net.http.JavaHttpBuilder.headers - Response-Header: Set-Cookie -> VUpersist=rd2480o00000000000000000000ffff0a4c0016o80; path=/; Httponly
groovyx.net.http.HttpException: Unauthorized
at groovyx.net.http.NativeHandlers.failure(NativeHandlers.java:69)
at groovy.lang.Closure.call(Closure.java:423)
at groovyx.net.http.fn.ClosureBiFunction.apply(ClosureBiFunction.java:45)
at groovyx.net.http.HttpBuilder$ResponseHandlerFunction.apply(HttpBuilder.java:1834)
at groovyx.net.http.JavaHttpBuilder$Action.lambda$execute$2(JavaHttpBuilder.java:157)
at groovyx.net.http.JavaHttpBuilder$ThreadLocalAuth.with(JavaHttpBuilder.java:320)
at groovyx.net.http.JavaHttpBuilder$Action.execute(JavaHttpBuilder.java:111)
at groovyx.net.http.JavaHttpBuilder.createAndExecute(JavaHttpBuilder.java:362)
at groovyx.net.http.JavaHttpBuilder.doGet(JavaHttpBuilder.java:369)
at groovyx.net.http.HttpObjectConfigImpl.nullInterceptor(HttpObjectConfigImpl.java:45)
at groovyx.net.http.HttpBuilder.get(HttpBuilder.java:360)
at groovyx.net.http.HttpBuilder.get(HttpBuilder.java:1311)
at GetD2LUserBySourcedIdSpec.test get /users call with d2lSourced Id(GetD2LUserBySourcedIdSpec.groovy:19)
Process finished with exit code -1
答案 0 :(得分:1)
当我第一次尝试使用这个库时,我自己也有同样的问题。 URI中不应包含任何路径,只包含协议和主机(和端口)
您的请求URI应如下所示:
request.uri = 'http://esbvucmv5.vu.edu.au'
get get中的Path看起来像这样:
request.uri.path = /D2L/2.0/users/
答案 1 :(得分:1)
感谢@Vinay提供的GitHub问题链接以及@cjstehno(有用的http-builder-ng项目的创建者)的支持,我在调用Mule时能够正常工作。
通常这应该在http-builder-ng
中有效const event = await Event.findById(eventId)
.populate('sport')
.populate('host')
.populate('players')
.exec()
但这对Mule不起作用,并且您收到有关身份验证的神秘错误消息,如上面的问题所示。
以下解决方法在调用Mule时解决了此问题。
request.auth.basic(username, password)