2017-06-13 46 views
-1

越来越坏请求,当我在POST方法请求体中发送 “/” 字符获取HTTP/1.1 400错误的请求[ R] [ n]的

HttpClient client = new HttpClient(); 
    PostMethod postMethod = new PostMethod(kairosDBUrl); 

    RequestEntity requestEntity = null; 

    try { 
     requestEntity = new StringRequestEntity(inputBody, "text/json", "UTF-8"); 
     postMethod.setRequestEntity(requestEntity); 
     client.executeMethod(postMethod); 
     responseBody = IOUtils.toString(postMethod.getResponseBodyAsStream(), postMethod.getResponseCharSet()); 
    } catch (IOException e) { 
     LOG.error("Unexpected exception : {}", e); 
    } finally{ 
     postMethod.releaseConnection(); 
    } 

输入:

{metrics=[{name=apache.Activity_Bytes/req, tags={host=[ip-10-0-0-201]}, aggregators=[{name=avg, align_sampling=true, sampling={value=5, unit=seconds}}]}], cache_time=0, start_absolute=1496222264000, end_absolute=1496224064000} 

错误:

"{"errors":["com.google.gson.stream.MalformedJsonException: Unterminated object at line 1 column 39"]}" 
+0

这只是一个疯狂的猜测,但你是否试图通过反斜杠斜线逃避斜线? – Jonathan

+1

你的JSON无效,你应该纠正它。 – Dorian

+0

JSON是完整的垃圾 - 抱歉。尝试http://json-validator.com/来更正您的输入。 –

回答

0

当您试图用方法无法处理的某些方法调用方法时,通常会出现“错误请求”状态。

在这种情况下,你得到这个异常

["com.google.gson.stream.MalformedJsonException: Unterminated object at line 1 column 39"] 

,告诉你,你以.json文件需要被固定。 我的建议是检查您的Json文件的结构是否与您的实体结构匹配。

+0

我尝试使用不带“/”字符的相同输入json。我也得到了回应。 –

+1

@madhukarreddy那么是什么让你认为“/”是你的问题的根源(而不是你的完全无效的JSON)? –

+0

在错误本身中,它显示了第1行和第39列。[“com.google.gson.stream.MalformedJsonException:未终止的对象在第1行第39列”] –

0

错误我应该使用json.toString(),但我用map.toString()返回无效的字符串。

map.toString()在没有特殊字符时工作,但当它有特殊字符时它不起作用。