I’ve been trying to solve this problem for a while now and I just can’t seem to make it work properly. I have this POST service that accepts JSON content:
public static void addLifestyleToRecipes(String categoryMappingList) {
Logger.info("In RecipeServices--> addLifestyleToRecipes");
Logger.info("JSON received: %s", categoryMappingList);
Type type = new TypeToken<List<CategoryMapping>>() {
}.getType();
List<CategoryMapping> categoryMapping = new Gson().fromJson(categoryMappingList, type);
for (CategoryMapping cat : categoryMapping) {
//the rest is irrelevant to the problem
}
When I test my method with a functionnal test, everything works fine :
List<CategoryMapping> list = new ArrayList<CategoryMapping>();
list.add(new CategoryMapping(MULTI_LANG_ID, recipes));
list.add(new CategoryMapping(MULTI_LANG_ID, recipes));
String body = new Gson().toJson(list);
Map<String, String> params = new HashMap<String, String>();
params.put("categoryMappingList", body);
StringBuffer url = TestHelper.generateBaseUrl(apiKey, apiVersion);
url.append("/recipes/add/categories");
Response response = POST(url, params);
The problem comes when I try to actually call the service, my parameter “categoryMappingList” is always null. I also tried using Chrome’s “Advanced Rest Client” to simulate a POST request but I always get the same result.
I also noticed that if I specified my Content-Type to be anything but “application/json; charset=utf-8”, I get a 403 error.
Any clues?
Thanks.
In your functional test you are adding the json as a parameter
If your client call is instead posting it in the body this will not be picked up in the categoryMappingList argument. To parse json from POST body