我有代码
HttpHeaders headers = new HttpHeaders();
headers.set("Current-Version", "v1");
LinkedMultiValueMap<String, String> map = new LinkedMultiValueMap<>();
map.add("email", "test@test.test");
HttpEntity<MultiValueMap<String, String>> request =
new HttpEntity<MultiValueMap<String, String>>(map, headers);
HttpEntity<Login> response = getRestTemplate().exchange(
url,
HttpMethod.POST,
request ,
Login.class
);
这很好,但在服务器端,我以格式JSON获得请求的BODY,尽管web格式是param1=val1¶m2=val2
。我必须以param1=val1¶m2=val2
格式向服务器发送发布请求。
我该怎么做?
答案 0 :(得分:1)
请使用以下方法。
from pandas.io.json import json_normalize
df = json_normalize(d, ['applications', ['answers']])
print (df)
answer question
0 Indeed How did you hear?
<强>用法强>
public static String getPostDataString(HashMap<Object, Object> params) throws UnsupportedEncodingException {
StringBuilder result = new StringBuilder();
boolean first = true;
for(Map.Entry<Object, Object> entry : params.entrySet()){
if (first)
first = false;
else
result.append("&");
result.append(URLEncoder.encode(entry.getKey().toString()));
result.append("=");
result.append(URLEncoder.encode(entry.getValue().toString()));
}
return result.toString();
}
将所有数据放入 try{
String requestJSON = getPostDataString(inputData);
}catch (UnsupportedEncodingException e){
e.printStackTrace();
}
并在此方法中传递HashMap
,您将获得 param1 = val1&amp; param2 = val2 格式的请求字符串。
示例强>
HashMap
现在在方法中传递HashMap<Object, Object> map = new HashMap<>();
map.put("email", "test@test.test");
map.put("password", "testtest");
map.put("type", "1");
以上。
String requestString =“”;
HashMap
您将获得以下格式的请求字符串。
<强> try{
requestString = getPostDataString(map);
}catch (UnsupportedEncodingException e){
e.printStackTrace();
}
强>
答案 1 :(得分:0)
在exchnage方法中,还有3个参数可以传递,
requestEntity - the entity (headers and/or body) to write to the request, may be null
responseType - the type of the return value
uriVariables - the variables to expand in the template