如何使用Grails RestBuilder发布用户名和密码

时间:2015-09-14 11:37:18

标签: grails

我正在尝试使用RestBuilder

执行以下操作
curl -X POST --header "Content-Type: application/xml;charset=UTF-8" --data "</xml_goes_here>" http://www.example.com/api/  -u username:password

我尝试了以下内容:

def rest = new RestBuilder()
        def url = 'http://www.example.com/api/?username=username&password=password'
        def resp = rest.post(url) {
            contentType("text/xml")
            body(xmlMessage)
        }

def rest = new RestBuilder()
        def url = 'http://username:password@www.example.com/api/'
        def resp = rest.post(url) {
            contentType("text/xml")
            body(xmlMessage)
        }

def rest = new RestBuilder()
        def url = 'http://www.example.com/api/'
        def resp = rest.post(url) {
            contentType("text/xml")
            setProperty("username","username")
            setProperty("password","password")
            body(xmlMessage)
        }

有人管理过吗?

1 个答案:

答案 0 :(得分:4)

尝试:

def resp = new RestBuilder().post(url) {
  auth username, password
  // the rest
}