'Java REST POST using Apache Client Issue
I am trying to create a web client to trigger my web service POST method that deals with a multipart request. The first part of the request deals with sending a name in JSON format, and then the second part is adding a file to submit. Any assistance would be greatly appreciated.
So I have the below service.
@RequestMapping(path = "/senddata", method = POST, consumes = { MediaType.MULTIPART_FORM_DATA_VALUE })
public ResponseEntity<Object> process(@RequestPart("person") Person person,
@RequestPart("document") MultipartFile document) {
System.out.println("/senddata called...");
System.out.println("person Name = " + person.getName());
System.out.println("File name = " + document.getName());
System.out.println("File Size = " + document.getSize());
person.setDocument(document);
Service.save(person);
return ResponseEntity.ok().build();
}
Then I have this as my client call:
public abstract class AbstractBaseRestClient {
public AbstractBaseRestClient() {
super();
}
protected HttpPost buildPostMultipartRequest(String uri) {
StringBuilder sb = new StringBuilder();
sb.append(BASE_URL).append(uri);
HttpPost httpPost = new HttpPost(sb.toString());
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
//builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
builder.addTextBody("person", "{name:Justin}", ContentType.APPLICATION_JSON);
File photo= new File("C:/path/to/logo.png");
try {
builder.addBinaryBody("document", new FileInputStream(photo), ContentType.APPLICATION_OCTET_STREAM, photo.getName());
} catch (IOException e) {
e.printStackTrace();
}
HttpEntity multipart = builder.build();
httpPost.setEntity(multipart);
return httpPost;
}
}
public class Client extends AbstractBaseRestClient {
public Client() { }
public String setImage() {
String uri = "/api/v1/upload";
int expectedResponseCode = 200;
HttpPost httpPost = buildPostMultipartRequest(uri);
return makeRestCall(httpPost, expectedResponseCode);
}
}
public class RunTestClient {
public static void main(String[] args) {
Client client = new Client();
String sendImage = client.setImage();
log(sendImage);
}
private static void log(String msg) {
System.out.println(msg);
}
}
My person object is:
public class Person{
private String name;
private MultipartFile document;
public Person() {
super();
}
public Person(String name) {
super();
System.out.println("Name: " + name);
this.name = name;
}
public Person(String name, MultipartFile document) {
super();
this.name = name;
this.document = document;
}
@Override
public String toString() {
return "Person [name=" + name + ", document=" + document + "]";
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public MultipartFile getDocument() {
return document;
}
public void setDocument(MultipartFile document) {
this.document = document;
}
}
When running this from the main, I get a 400 error saying:
Incorrect Response Status:: 400. Expected: [200]
The content type is not application/json. It is: text/html; charset=UTF-8
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Draft//EN"><HTML><HEAD><TITLE>Error 400--Bad Request</TITLE></HEAD><BODY bgcolor="white"><FONT FACE=Helvetica><BR CLEAR=all><TABLE border=0 cellspacing=5><TR><TD><BR CLEAR=all><FONT FACE="Helvetica" COLOR="black" SIZE="3"><H2>Error 400--Bad Request</H2></FONT></TD></TR></TABLE><TABLE border=0 width=100% cellpadding=10><TR><TD VALIGN=top WIDTH=100% BGCOLOR=white><FONT FACE="Courier New"><FONT FACE="Helvetica" SIZE="3"><H3>From RFC 2068 <i>Hypertext Transfer Protocol -- HTTP/1.1</i>:</H3></FONT><FONT FACE="Helvetica" SIZE="3"><H4>10.4.1 400 Bad Request</H4></FONT><P><FONT FACE="Courier New">The request could not be understood by the server due to malformed syntax. The client SHOULD NOT repeat the request without modifications.</FONT></P></FONT></TD></TR></TABLE></BODY></HTML>
Does anyone see where I am going wrong? Any assistance would be greatly appreciated as I have not been able to figure this out yet. I am not sure why my request is not being accepted.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
