I am trying to create an imposter using java, but it's failing with an exception, some how it's working fine for HTML and XML types but not with JSON, need help, below is the JSON, JAVA code and Exception.
my imposter -
{
"port": "9091",
"protocol": "http",
"stubs": [
{
"responses": [
{
"is": {
"headers": {
"Content-Type": "application/json"
},
"statusCode": 200,
"body": "{
\"type\": \"simpleRefNo\"
}"
}
}
],
"predicates": [
{
"and": [
{
"equals": {
"method": "GET"
}
},
{
"contains": {
"path": "fortune_cookie"
}
},
{
"deepEquals": {
"query": {
"type": "latest"
}
}
}
]
}
]
}
]
}
my Java code -
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import javax.net.ssl.HttpsURLConnection;
public class Test {
public static void main(String[] args) throws Exception {
String t =getStringFromInputStream( );
// System.out.println(t);
sendPost( t);
}
private static String getStringFromInputStream( ) throws IOException {
// The name of the file to open.
Path path = FileSystems.getDefault().getPath("//TestFactory//mbjson//", "newres1.json");
String contents = new String(Files.readAllBytes(path), StandardCharsets.UTF_8);
return contents;
}
private static void sendPost(String urlParameter) throws Exception {
String url = "http://127.0.0.1:2525/imposters";
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
//add reuqest header
con.setRequestMethod("POST");
con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
con.setRequestProperty("Content-Type","application/json");
String urlParameters = urlParameter;
// Send post request
con.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(urlParameters);
wr.flush();
wr.close();
int responseCode = con.getResponseCode();
System.out.println("\nSending 'POST' request to URL : " + url);
System.out.println("Post parameters : " + urlParameters);
System.out.println("Response Code : " + responseCode);
BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
//print result
System.out.println(response.toString());
}
}
Exception I'm facing -
Exception in thread "main" java.io.IOException: Server returned HTTP response code: 400 for URL: http://127.0.0.1:2525/imposters
"protocol": "http",
"stubs": [
{
"responses": [
{
"is": {
"headers": {
"Content-type": "text/html"
},
"statusCode": 200,
"body": "get a job"
}
}
],
"predicates": [
{
"and": [
{
"equals": {
"method": "GET"
}
},
{
"contains": {
"path": "fortune_cookie"
}
},
{
"deepEquals": {
"query": {
"type": "latest"
}
}
}
]
}
]
}
]
}
Response Code : 201
--
You received this message because you are subscribed to the Google Groups "mountebank-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mountebank-disc...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
"body": "{
\"type\": \"simpleRefNo\"
}"
Proper JSON would be:
"body": "{\n\"type\": \"simpleRefNo\"\n}"
The next release will accept pure JSON objects for the body field, making that easier to type. I'll release it as soon as I can (recovering from surgery at the moment).
-Brandon
Thanks, Got it now!