I am trying to post
multipart form-data
request in java to api. I am getting 400
http error code. Below is my end point in spring:
spring boot api
@org.springframework.web.bind.annotation.PostMapping(value="register2")
public ResponseEntity<CoreResponseHandler> doRegister2(@RequestPart String json) {
System.out.println("~~~~~~ "+json+" ~~~~~~");
return null;
}
core java code to consume above api
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import org.json.JSONException;
import org.json.JSONObject;
public class Test {
public static void main(String[] args) {
try {
URL url = new URL("http://localhost:8012/register2");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW");
conn.setRequestProperty("charset","utf-8");
conn.setRequestProperty("Content-Disposition","form-data;name='json'");
conn.setDoOutput(true);
conn.setDoInput(true);
JSONObject jsonObject = buidJsonObject();
setPostRequestContent(conn, jsonObject);
conn.connect();
System.out.println(conn.getResponseCode());
if(conn.getResponseCode()==200){
BufferedReader reader= new BufferedReader(new InputStreamReader(conn.getInputStream()));
StringBuilder stringBuilder = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null)
{
stringBuilder.append(line + "\n");
}
System.out.println(stringBuilder+" <<<<<<<<<<<<<<<<<<<<<<<<<" );
jsonObject=new JSONObject(stringBuilder.toString());
}
else{
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getErrorStream()));
StringBuilder stringBuilder = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null)
{
stringBuilder.append(line + "\n");
}
System.out.println(stringBuilder+" <<<<<<<<<<<<<<<<<<<<<<<<<" );
System.out.println("ERRORERRORERRORERRORERRORERRORERRORERRORERROR"+conn.getResponseCode());
}
}catch(Exception ex) {
ex.printStackTrace();
}
}
private static JSONObject buidJsonObject() throws JSONException {
JSONObject jsonObject = new JSONObject();
jsonObject.accumulate("username", "13338912");
jsonObject.accumulate("encryptedPassword", "encpass");
jsonObject.accumulate("name", "fish");
jsonObject.accumulate("email", "fish@fish.com");
jsonObject.accumulate("status", "active");
return jsonObject;
}
private static void setPostRequestContent(HttpURLConnection conn,
JSONObject jsonObject) throws IOException {
OutputStream os = conn.getOutputStream();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
writer.write(jsonObject.toString());
writer.flush();
writer.close();
os.close();
}
}
below is the output that I got:
output
400
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Tue Oct 09 13:51:00 IST 2018There was an unexpected error (type=Bad Request, status=400).Required request part 'json' is not present <<<<<<<<<<<<<<<<<<<<<<<<< ERRORERRORERRORERRORERRORERRORERRORERRORERROR400 I don't
want to use 3rd party dependencies like okkhttp
etc.
where is my logic wrong. kindly correct me. Why am I not getting http
status 200
~~~~~~~~~~~~~problem seems to be unsolvable~~~~~~~~~~~~~
@org.springframework.web.bind.annotation.PostMapping(value="register2" ,consumes = MediaType.APPLICATION_JSON,
produces = MediaType.APPLICATION_JSON)
public ResponseEntity<CoreResponseHandler> doRegister2(@RequestPart String json) {
System.out.println("~~~~~~ "+json+" ~~~~~~");
return null;
}
It's also a solution please try this one
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.