Pregunta

I have a file that I want to upload so I have your standard MultipartEntityBuilder like this:

MultipartEntityBuilder multiPartEntity = MultipartEntityBuilder.create();
multiPartEntity.addBinaryBody("file", file);

I also have some form params that I send with the POST like this:

ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
postParameters.add(new BasicNameValuePair("userId",userId));
postParameters.add(new BasicNameValuePair("taskId",taskId));
new UrlEncodedFormEntity(postParameters)

Both work individually, My question how do I do both in one call? I need to fold one into the other so I can make this in one HttpPost() call.

¿Fue útil?

Solución

You seem to be confusing the application/x-www-form-urlencoded and multipart/form-data content types. When sending a multipart request, you are using multipart/form-data, in which case you don't need to URL encode the content. Just set the text directly

MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create();
multipartEntityBuilder.addBinaryBody("file", file);
multipartEntityBuilder.addTextBody("userId", "someIdWith@url$encodable<>characters");

See the specification for more details.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top