oblac / jodd

Jodd! Lightweight. Java. Zero dependencies. Use what you like.

Home Page:https://jodd.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

jodd-http: File upload issue

srinivas-n4u opened this issue · comments

Current behavior

Hello, I am trying to use Jodd-http to send http post request for to server with file upload. ( https://www.ibm.com/support/knowledgecenter/SS4SVW_3.0.0/administering/deploy_api.html ) Which is basically a http post to upload a file in request body with content-type set to application/zip. When i tried this from jodd-http using form("file", new File("artifactpath")) server is not accepting the request saying "415 Unsupported media type Content-Type is not application/zip". Below is the code i am trying to use -

SocketHttpConnectionProvider shcp = new SocketHttpConnectionProvider();
shcp.setSslProtocol("SSL")

def uri = "https://Server/services"
HttpResponse response = HttpRequest.post(uri)
                                   .withConnectionProvider(shcp)
                                   .basicAuthentication(username,password)     
                                   .trustAllCerts(true)
                                   .header("Content-Type","application/zip")
                                   .form("mimeType","application/zip")
                                   .form("file", new File(artifactPath))                                     
                                   .send()

Can you help me with an example to do the http post with attachment in request body (Please see the request details in ibm link above) ?

Expected behavior

I am able to send http post to the server using postman successfully using below binary option.
image

Steps to Reproduce the Problem

It might not be a issue with jodd, i just need some guidance on how to do this kind of requests.

Thanks for the help..
Srinivas N

Can you please get me the curl export of above request? You are using the binary, so I assume that binary content is sent as a body, meaning you can't use form().

You can try to use body() instead - but it accepts a String, so you would need to convert the zip to correctly encoded String, what may be tricky. I will add an overloaded method body() that accepts the byte array or file.

Thanks for quick response Igr.. I am able to use this method - body(byte[] content, java.lang.String contentType) and send the post request successfully. If we can send file directly via body, that will be great.

Yes, that is the correct method @srinivas-n4u
It just sets the body with raw content

	 * Sets <b>raw</b> body content and discards form parameters.
	 * Also sets "Content-Length" and "Content-Type" parameter.

Sorry, I didn't saw it on my quick scan :)