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

**URL:** <https://devforum.zoom.us/t/how-to-post-a-file-in-contacts-chat-using-java/83444>\
**Category:** Chat\
**Tags:** java\
**Created:** [February 20, 2023, 5:03pm UTC](https://devforum.zoom.us/t/how-to-post-a-file-in-contacts-chat-using-java/83444 "2023-02-20T17:03:44Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![ced](https://sea2.discourse-cdn.com/flex016/user_avatar/devforum.zoom.us/ced/32/45279_2.png) [@ced](https://devforum.zoom.us/u/ced)\
**Post date:** [February 20, 2023, 5:03pm UTC](https://devforum.zoom.us/t/how-to-post-a-file-in-contacts-chat-using-java/83444/1 "2023-02-20T17:03:45Z")

</div>

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.

```auto
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();}
	}
}

```

---

<div class="post-metadata">

**Author:** ![ced](https://sea2.discourse-cdn.com/flex016/user_avatar/devforum.zoom.us/ced/32/45279_2.png) [@ced](https://devforum.zoom.us/u/ced)\
**Post date:** [February 27, 2023, 2:03pm UTC](https://devforum.zoom.us/t/how-to-post-a-file-in-contacts-chat-using-java/83444/2 "2023-02-27T14:03:09Z")

</div>

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.

```auto
		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();

```

---

<div class="post-metadata">

**Author:** ![elisa.zoom](https://sea2.discourse-cdn.com/flex016/user_avatar/devforum.zoom.us/elisa.zoom/32/47836_2.png) [@elisa.zoom](https://devforum.zoom.us/u/elisa.zoom)\
**Post date:** [March 1, 2023, 4:02pm UTC](https://devforum.zoom.us/t/how-to-post-a-file-in-contacts-chat-using-java/83444/3 "2023-03-01T16:02:30Z")

</div>

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?

> [@How to user Server-to-Sever OAuth app with Postman](https://devforum.zoom.us/t/how-to-user-server-to-sever-oauth-app-with-postman/70848):
>
> How to use our Server-to-Server OAuth with Postman? As some of you might be aware of, our JWT app is being deprecated by June 2023 and we encourage you to migrate to the newly introduced [Server-to-Server OAuth App](https://marketplace.zoom.us/docs/guides/build/server-to-server-oauth-app) That being said, here is a quick and simple guide on how to use or new app with Postman As stated in our documentation here: This new app type facilitates OAuth-authenticated requests between servers without end-user involvement. And this grant type enable your private server a…

Hope this helps,  
Elisa

---

<div class="post-metadata">

**Author:** ![ced](https://sea2.discourse-cdn.com/flex016/user_avatar/devforum.zoom.us/ced/32/45279_2.png) [@ced](https://devforum.zoom.us/u/ced)\
**Post date:** [March 1, 2023, 4:16pm UTC](https://devforum.zoom.us/t/how-to-post-a-file-in-contacts-chat-using-java/83444/4 "2023-03-01T16:16:19Z")

</div>

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](https://api.zoom.us/v2/chat/users/%7BuserId%7D/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.

---

<div class="post-metadata">

**Author:** ![elisa.zoom](https://sea2.discourse-cdn.com/flex016/user_avatar/devforum.zoom.us/elisa.zoom/32/47836_2.png) [@elisa.zoom](https://devforum.zoom.us/u/elisa.zoom)\
**Post date:** [March 1, 2023, 4:48pm UTC](https://devforum.zoom.us/t/how-to-post-a-file-in-contacts-chat-using-java/83444/5 "2023-03-01T16:48:04Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ced](https://sea2.discourse-cdn.com/flex016/user_avatar/devforum.zoom.us/ced/32/45279_2.png) [@ced](https://devforum.zoom.us/u/ced)\
**Post date:** [March 1, 2023, 5:00pm UTC](https://devforum.zoom.us/t/how-to-post-a-file-in-contacts-chat-using-java/83444/6 "2023-03-01T17:00:26Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![elisa.zoom](https://sea2.discourse-cdn.com/flex016/user_avatar/devforum.zoom.us/elisa.zoom/32/47836_2.png) [@elisa.zoom](https://devforum.zoom.us/u/elisa.zoom)\
**Post date:** [March 1, 2023, 8:53pm UTC](https://devforum.zoom.us/t/how-to-post-a-file-in-contacts-chat-using-java/83444/7 "2023-03-01T20:53:34Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ced](https://sea2.discourse-cdn.com/flex016/user_avatar/devforum.zoom.us/ced/32/45279_2.png) [@ced](https://devforum.zoom.us/u/ced)\
**Post date:** [March 2, 2023, 7:18am UTC](https://devforum.zoom.us/t/how-to-post-a-file-in-contacts-chat-using-java/83444/8 "2023-03-02T07:18:02Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ced](https://sea2.discourse-cdn.com/flex016/user_avatar/devforum.zoom.us/ced/32/45279_2.png) [@ced](https://devforum.zoom.us/u/ced)\
**Post date:** [March 16, 2023, 4:24pm UTC](https://devforum.zoom.us/t/how-to-post-a-file-in-contacts-chat-using-java/83444/9 "2023-03-16T16:24:35Z")

</div>

After some tries I have found myself a solution…

> <https://stackoverflow.com/questions/75475845/send-document-to-zoom-chat-via-java-program/75748227#75748227>

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