How to post a file in contact's chat using java?

I’m trying to send a file in a contact’s chat using oauth server-to-server token (bearer).
I’ve tried using simple java classes but I always receive 405 Method Not Allowed error.
Here’s my code that uses apache http client.

import org.apache.http.HttpEntity;
import org.apache.http.HttpHeaders;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

public class ZoomNewMultipartFileSend {
	private static final String API_ENDPOINT = "https://api.zoom.us/v2/chat/users/";
	public static void main(String[] args) {
		String token = ZoomApi.getToken();//get a token using oauth server-to-server
		String userId = "my userid";
		String toContact = "rec.contact userid";
		String filePath = "path to my file with extension";
		try {
			CloseableHttpClient httpClient = HttpClients.createDefault();
			HttpPost uploadFile = new HttpPost(API_ENDPOINT+userId+"/messages/files");
			uploadFile.setHeader(HttpHeaders.AUTHORIZATION,"Bearer "+token);
			MultipartEntityBuilder builder = MultipartEntityBuilder.create();
			builder.addTextBody("to_contact",toContact);
			File f=new File(filePath);
			builder.addBinaryBody("files",f,ContentType.APPLICATION_OCTET_STREAM, filePath);
			HttpEntity multipart = builder.build();
			uploadFile.setEntity(multipart);
			CloseableHttpResponse response = httpClient.execute(uploadFile);
			HttpEntity responseEntity = response.getEntity();
			<read response>
		}catch(Exception e) {e.printStackTrace();}
	}
}

Another implementation, still not working. With this I receive 200 response code but I don’t receive anything. i should receive 201 code and message id in responsemesage.

		String accessToken = ZoomApi.getToken();
		String recipientId = "Nsn6KsSNT_Kr6FhZce5WVA"; // Inserisci l'ID del destinatario del messaggio
		File file = new File("u:\\LibroCod.pdf"); // Inserisci il percorso del file che vuoi inviare
		OkHttpClient client = new OkHttpClient.Builder()
				.followRedirects(true)
				.build();
		// Crea il body della richiesta multipart
		RequestBody requestBody = new MultipartBody.Builder()
				.setType(MultipartBody.FORM)
				.addFormDataPart("to_contact", recipientId)
				.addFormDataPart("files", file.getName(), RequestBody.create(file, MediaType.parse("application/pdf")))
				.build();
		Request request = new Request.Builder()
				.url("https://api.zoom.us/v2/users/dumhyFISSMK3nWOopbChHg/messages/files")
				.addHeader("Authorization", "Bearer " + accessToken)
				//.addHeader("to_contact", recipientId)
				.post(requestBody)
				.build();
		// Invia la richiesta e ottieni la risposta
		Response response = client.newCall(request).execute();

HI @ced
Thanks for reaching out to the Zoom Developer Forum, I am happy to help here
Have you tried replicating this issue using Postman to confirm that you are generating the Access token correctly and also posting this message correctly?

Hope this helps,
Elisa

Amazing post, Elisa. I am already able to generate tokens, I use them everyday to send messages to my contacts with api “https://api.zoom.us/v2/chat/users/{userId}/messages” but for some reason I can’t send files. I have tried everything… I receive 200 response that seems good, but I should get 201 with id in response, and I don’t receive anything.

Thanks for confirming this @ced
Could you please confirm that this was working in the past? that you were able to send files?
In the meantime, I will do some troubleshooting on my end.

Best,
Elisa

Tokens are good, but the program is new. I am new to rest and webhooks, I am using the program to send simple richtext messages since a month and they’re working like a charm, but I can’t find a way to make this program to work.

So you have never been able to send files via API @ced ?

Yes, I’m still trying to @elisa.zoom .

After some tries I have found myself a solution…

It seems Zoom has updated its api URL using “file” prefix instead of “api” and now this code is working.