Free Support Forum -

Aspose word cloud cURL how to mail merge, can't get it to work


I’m setting up the environment for Java REST API Call example with OkHttp REST Client. I will share the example and its details asap.


Here is a sample code snippet to call MailMergeOnline API method using OkHttp REST Client. Hopefully it will help you accomplish the task.

import java.nio.file.Files;

import org.json.JSONObject;

import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class OKHttpRestAPITest {
    // Get App Key and App SID from
	static String App_Key = "xxxxxxxxxxxxxxxxxxxxxxxxxxx";
	static String App_SID = "xxxxxx-xxxx-xxxx-xxxxx-xxxxxxx";

	public static void main(String[] args) throws IOException {



	public static void MailMergeOnline() throws IOException {
		// Get JWT Token
		String access_token = JWTAuth(App_Key, App_SID);
		OkHttpClient client = new OkHttpClient();
		String url = "";
		File TemplateFile = new File("C:/Temp/SampleMailMergeTemplate.docx");
		File DataFile = new File("C:/Temp/SampleMailMergeTemplateData.txt");

		RequestBody body = new MultipartBody.Builder().setType(MultipartBody.FORM)
				.addFormDataPart("Template", "SampleMailMergeTemplate.docx", RequestBody.create(
						MediaType.parse("application/vnd.openxmlformats-officedocument.wordprocessing"), TemplateFile))
				.addFormDataPart("Data", "SampleMailMergeTemplateData.txt",
						RequestBody.create(MediaType.parse("txt/plain"), DataFile))
		Request request = new Request.Builder().url(url).header("Content-Type", "multipart/form-data")
				.addHeader("Accept", "application/octet-stream")
						"Bearer "+access_token)

		Response response = client.newCall(request).execute();
		// Save Response Stream to local 
		InputStream in = response.body().byteStream();
		File target = new File("C:/Temp/MailMerge.docx");
		Files.copy(in, target.toPath(), java.nio.file.StandardCopyOption.REPLACE_EXISTING);


	public static String JWTAuth(String appkey, String appsid) throws IOException {
		OkHttpClient client = new OkHttpClient();
		String url = "";

		RequestBody body = new FormBody.Builder().add("grant_type", "client_credentials").add("client_id", appsid)
				.add("client_secret", appkey).build();
		Request request = new Request.Builder().url(url).header("Content-Type", "application/x-www-form-urlencoded")
				.addHeader("Accept", "application/json").post(body).build();

		Response response = client.newCall(request).execute();
		JSONObject jsonObject = new JSONObject(response.body().string());
		return jsonObject.get("access_token").toString();



Thankyou very much!
I’m just not getting it to work just yet.
I have 2 of the same warning because some create method is deprecated…
Screenshot_1.png (6.5 KB)
Screenshot_2.png (116.6 KB)
Also, when i try to start it, I’m getting an error:
Screenshot_3.png (12.8 KB)
Im running latest okhttp3 version , 4.9.0.
Hopefully you can see what i did wrong.


I have used the OkHttp 3.9 version with JDK 1.8 in above shared example. For the OkHttp3 4.9.0 issue please swap the arguments of the create method as following it will resolve the deprecation warning.

RequestBody body = new MultipartBody.Builder().setType(MultipartBody.FORM)
		.addFormDataPart("Template", "SampleMailMergeTemplate.docx", 
				RequestBody.create(TemplateFile, MediaType.parse("application/vnd.openxmlformats-officedocument.wordprocessing")))
		.addFormDataPart("Data", "SampleMailMergeTemplateData.txt",
				RequestBody.create(DataFile, MediaType.parse("txt/plain")))

However for the second error. Please google the error and try the suggested solutions. Hopefully, it will resolve the issue.

Thankyou very much!
I managed to get the merge done without any issues!!.

Futhermore I would like to know if it’s possible to upload a word document just like the mail merge but then with the feature Replace document text.
As you can see in the cURL example the word file is (i think) located in the aspose cloud storage it self and there is only one word that’s to be replaced.

I would like to implement this like you upload a file (just like the mail merge) and can change multiple values with new values. In the example you see one valua that’s being changed, but i would like to know if you can just change any number of values for different ones.

1 Like


Thanks for your feedback. I have moved your text replacement query to a new post.