首页 > 解决方案 > 如何测试 Unirest 获得响应

问题描述

我有以下 Http 获取请求,它与其他 API 一起使用,我现在需要从 UNirest 框架中使用

我原来的休息电话如下

def http = new HTTPBuilder(graph_base_user_url + "?")

        http.request(GET) {

            requestContentType = ContentType.JSON
            //uri.query = [ $filter:"mail eq '$userEmail'"].toString()
            uri.query=[$filter:"mail eq '$userEmail'"]
           
            headers.'Authorization' = "Bearer " + AuthToken    

            response.success = { resp, json ->
                //as the retunr json alue is an array collection we need to get the first element as we request all time one record from the filter
                if (json.value){
                _userId=json.value[0].id    
                } 
                else
                _userId=-1 // user does not exist
                                    
            }

            // user ID not found : error 404
            response.'404' = { resp ->       
                _userId = 'Not Found'
            }

        }
        _userId

然后我需要将其转换为 UNirest 调用。为此,我从以下内容开始:

 def result = post(graph_base_user_url + "?")
             .headers("Authorization","Bearer " + AuthToken )
             .queryString("$filter","mail eq '$userEmail'")
             .asJson()
            
    
            **response.success = { resp, json ->
                
                if (json.value){
                _userId=json.value[0].id    
                } 
                else
                _userId=-1 // user does not exist
                                    
            }
            // user ID not found : error 404
            response.'404' = { resp ->       
                _userId = 'Not Found'
            }**

        }
        _userId

如何更改 Bold 中的代码以与 UNIRest 兼容?感谢帮助

问候

标签: groovyunirest

解决方案


推荐阅读