本文介绍了当我在Grails上调用Post方法时,参数是空的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我的应用程序编写webservices。
我的问题是,当我使用GET方法调用它时,它可以工作,但是当我使用POST方法时,params不包含我的参数的蚂蚁:

当我使用GET调用时,这是params的内容:

  params:[username:azerty,test:test,param2: param2,action:editProfile,controller:userWebService] 

当我使用POST调用时,这是params:

  params:[action:editProfile,controller:userWebService] 

编辑:

  / *用户控制器* / 
/ service / $ action?(controller:userWebService,parseRequest:true)

UserWebServiceController

  .... 
static allowedMethods = [editProfile:['POST ','GET']]
....
def editProfile(){

printlnparams:+ params
....
}

测试我在Chrome中使用REST控制台插件




解决方案

params 不作为查询字符串发送在 POST 请求中,不像 GET 。如果 POST 请求,参数字符串必须嵌入到请求正文中。



使用 content-type : application / x-www-form-urlencoded



并在 request-body 中使用

username = azerty& test = test



您应该能够看到 params 控制器内的参数。




你应该能够看到

params:[age:25,name:Hamila,action:editProfile,controller:userWebService]



我在测试中让你看起来更年轻吗? :)


i'm writing webservices for my application.my problem is that when i call it using GET method it works , but when i use the POST method params doesn't contains ant of my parameters:

when i call using GET , this is the content of params :

params : [username:azerty, test:test, param2:param2, action:editProfile, controller:userWebService]

when i call using POST, this is the content of params :

params : [action:editProfile, controller:userWebService]

EDIT :

/* user controller */
"/service/$action?"(controller: "userWebService", parseRequest: true)

in UserWebServiceController

....
static allowedMethods = [editProfile:['POST', 'GET']]
....
def editProfile(){

    println "params : "+ params
....
}

to test i'm using REST console plugin in chrome

解决方案

params are not sent as a query string in POST requests unlike GET. The parameter strings has to be embedded in the request body in case of POST request.

Use content-type: application/x-www-form-urlencoded

and in the request-body use
username=azerty&test=test

You should be able to see the parameters in params inside controller.

You should be able to see
params : [age:25, name:Hamila, action:editProfile, controller:userWebService]

Did I make you look younger in the test? :)

这篇关于当我在Grails上调用Post方法时,参数是空的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 13:37
查看更多