user3872094
user3872094

Reputation: 3351

confused sending json as a parameter

I've the below java program that prints a json string.

System.out.println(objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(objectsToSerialise));

I tried to find the type of objectsToSerialise with the below line of code.

    System.out.println(objectsToSerialise.getClass().getTypeName());

this returned java.util.ArrayList

the output that I get is as below.

[ {
  "EntityLabels" : [ {
    "StartToken" : 8,
    "EntityType" : "Personname",
    "EndToken" : 16
  }, {
    "StartToken" : 24,
    "EntityType" : "Amount::Spent",
    "EndToken" : 31
  } ],
  "ExampleText" : "What is Frede's limit? ",
  "SelectedIntentName" : "GiftLimit"
} ]

And I'm trying to post the same to my API . And the code is as below.

And luisUrl ="https://westus.api.cognitive.microsoft.com"

HttpPost httpPost = new HttpPost(luisUrl + "/luis/v1.0/prog/apps/" + appId + "/examples?");
httpPost.setHeader("Ocp-Apim-Subscription-Key", subsriptionId);
StringEntity params = new StringEntity(objectsToSerialise.toString());
httpPost.setHeader("Content-Type", "application/json");
httpPost.setEntity(params);
HttpResponse response = httpclient.execute(httpPost);
        /* Debug Info */
System.out.println("---------------Start2----------------");
HttpEntity entity = response.getEntity();
String responseString = EntityUtils.toString(entity, "UTF-8");
System.out.println(responseString);
System.out.println("---------------End----------------");
        /*End Debug Info*/
if (response.getStatusLine().getStatusCode() == 201) {
        success = true;
        System.out.println("Success");
} else {
        System.out.println("Block");
        success = false;
}

And this is always printing else content. i.e. Block. I thought this might be an issue with my calling and tried the same in Postman. with the below details.

URL:https://westus.api.cognitive.microsoft.com/luis/v1.0/prog/apps/{appId}/examples?

Method: post

Headers:

Content-Type:application/json
Ocp-Apim-Subscription-Key:{mySubscriptionKey}

Body:**raw->JSON **Content:

[ {
  "EntityLabels" : [ {
    "StartToken" : 8,
    "EntityType" : "Personname",
    "EndToken" : 16
  }, {
    "StartToken" : 24,
    "EntityType" : "Amount::Spent",
    "EndToken" : 31
  } ],
  "ExampleText" : "What is Frede's limit? ",
  "SelectedIntentName" : "GiftLimit"
} ]

To my surprise, when I post this, it is returning me 201, where as my Java code is returning me 400.

This is very confusing. Please let me know where am I going wrong and how can I fix this.

Thanks

Upvotes: 0

Views: 51

Answers (1)

Roman Puchkovskiy
Roman Puchkovskiy

Reputation: 11835

objectsToSerialize seems to be an ArrayList, but in your code you construct request body with new StringEntity(objectsToSerialise.toString());. That is, you don't convert your ArrayList to JSON, but instead you just get its string representation which is not a JSON usually.

Try changing

new StringEntity(objectsToSerialise.toString());

to

new StringEntity(objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(objectsToSerialise));

Upvotes: 2

Related Questions