Reputation: 3852
I created a rest web service with Grails 2.3.x as below:
import grails.rest.RestfulController
class CityController extends RestfulController{
static responseFormats = ['json', 'xml']
CityController() {
super(City)
}
}
Here is my domain class:
import groovy.transform.ToString
import groovy.transform.EqualsAndHashCode
/**
* City
* A domain class describes the data object and it's mapping to the database
*/
@ToString(includeNames = true, includeFields = true, excludes = 'dateCreated,lastUpdated,metaClass')
@EqualsAndHashCode
class City {
/* Default (injected) attributes of GORM */
Long id
Long version
/* Automatic timestamping of GORM */
Date dateCreated
Date lastUpdated
String cityName
String postalCode
String countryCode // either iso2 or iso3
static constraints = {
postalCode blank:false, nullable:false
cityName blank:false, nullable:false
countryCode minSize:2, maxSize:3, blank:false, nullable:false, matches: "[A-Z]+"
}
}
and this is urlMapping
class UrlMappings {
static mappings = {
"/$controller/$action?/$id?(.$format)?"{
constraints {
// apply constraints here
}
}
"/"(view:"/index")
"500"(view:'/error')
//
// RESTService api
"/api/city"(resources: 'city')
}
}
when I try to get my data with curl, I manage to get my result using this line:
curl -X GET -H "Accept:application/json" http://localhost:8080/ComuneUtenti/city
But when I try to post data to my WS, I run into an error as below:
curl: (6) Could not resolve host: countryCode
curl: (6) Could not resolve host: postalCode
curl: (3) [globbing] unmatched close brace/bracket in column 6
{"errors":[{"object":"comuneutenti.City","field":"postalCode","rejected-value":null,"message":"property [postalCode] of class [class comuneutenti.City] cannot be null"},{"object":"comuneutenti.City","field":"cityName","rejected-value":null,"message":"property
[cityName] of class [class comuneutenti.City] cannot be null"},{"object":"comuneutenti.City","field":"countryCode","rejected-value":null,"message":"property[countryCode] of class [class comuneutenti.City] cannot be null"}]}
here is my POST:
curl -H "Content-Type:application/json" -X POST -d '{"cityName":"NewYork", "countryCode":"IT", "postalCode": "00166"}' http://localhost:8080/ComuneUtenti/api/city
What am I missing?
I am using Grails 2.3.10 The same WS can be called with Postman.
Thanks
Upvotes: 2
Views: 4201
Reputation: 1121
Did you try erichelgeson's suggestion?
The correct curl method is
curl -H "Content-Type:application/json" -X POST -d '{"cityName":"NewYork", "countryCode":"IT", "postalCode": "00166"}' http://localhost:8080/ComuneUtenti/api/city
Notice the single quotes around the json payload.
To see what's really going on you can use the echo command.
$ echo {"hello":"world"}
{hello:world}
$ echo '{"hello":"world"}'
{"hello":"world"}
The shell is eating the quotes around your fields.
Upvotes: 2
Reputation: 1121
What version of grails 2.3 are you using? Grails 2.3.8 had a bug where you could not post to rest services.
I believe this was the checked in here.
https://github.com/grails/grails-core/commit/c050212fa0992f5b2d62c8e63795e4278fb2c3f4
Upvotes: 0