开发者

Posting JSON with curl to Grails JSON REST API plugin

开发者 https://www.devze.com 2023-03-28 09:54 出处:网络
I have a Grails app running on 1.3.7 with the json-rest-api plugin version 1.0.8 installed. I\'m trying to post data from the command line using curl in order to create a new instance of a domain clas

I have a Grails app running on 1.3.7 with the json-rest-api plugin version 1.0.8 installed. I'm trying to post data from the command line using curl in order to create a new instance of a domain class, but cannot seem to figure out the correct way to format the JSON so that the parser is satisfied. I can't find any documentation either that would conclusively describe how the data is supposed to be formatted.

The domain class is defined like this:

package foo

class Foo {
  static expose = 'foo'

  String name

  static constraints = {
    name(inList: ['xyzzy', 'quux'])
  }
}

Given the above, this doesn't work:

$ curl -X POST --header "Content-Type: application/json" --data-urlencode '{"name":"x开发者_开发技巧yzzy"}' http://myapp/api/foo

The app returns a 500 error code and the content of the reply is

{"success":false,"message":"Property [name] of class [class foo.Foo] cannot be null"}

What kind of data does the API expect me to send?


Try the following format:

{ data: { name: "Blabla" } }


This isn't specific to the json-rest API. I've only ever used the built in RESTful capabilities of Grails.

You need your URL Mapping so that parseRequest = true. You have to include the class in your JSON:

{"class":"Foo", "name":"blabla"}

In your controller, you do this...

def fooInstance = new Foo(params['foo'])

And your gold.

0

精彩评论

暂无评论...
验证码 换一张
取 消