0

I'm trying to do a post to an endpoint and set some form params and a file using AsyncHttp.

This is the code I have:

httpClient.preparePost(url)
    .addHeader(HttpHeaders.AUTHORIZATION, authorizationToken)
    .addHeader(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE)
    .addFormParam("type", type)
    .addBodyPart(new ByteArrayPart("file", file.getBytes()))
    .execute()
    .get();

Right now I am getting 4xx, but if I do it with postman it works and I get 201.

What am I doing wrong?

Slava Vedenin
  • 58,326
  • 13
  • 40
  • 59
Manuelarte
  • 1,658
  • 2
  • 28
  • 47

1 Answers1

0

In my opinion, you need to use or json (without form params and sending file or use multipart/form-data POST instead of json).

So, you can replace:

.addHeader(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE)

to:

.addHeader(HttpHeaders.ACCEPT, MediaType.MULTIPART_FORM_DATA)

Or (if you really need json) you should delete .addFormParam("type", type) and use something like this:

httpClient.preparePost(url)
    .addHeader(HttpHeaders.AUTHORIZATION, authorizationToken)
    .addHeader(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE)
    .setBody(jsonContent)
    .execute()
    .get();

When jsonContent is your json content. Also see this question: Posting a File and Associated Data to a RESTful WebService preferably as JSON.

Slava Vedenin
  • 58,326
  • 13
  • 40
  • 59