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

It doesn’t work for me:
Screenshot_9.png (17.5 KB)
Changed the output name but still 0 recieved
The new file is still an empty word document

@niek.moor

It is quite strange. Can you please confirm your git bash version and your OS details? We will try to replicate the issue at our end. Meanwhile, you may please try the API call in Postman?

Did file rename of download file to xxx.docx work for you?

Hi, my git bash its the newest version, so that will be 2.28.0 64-bit for windows 10
When trying with postman, I import the raw data and i’m getting status 401. Do i need to edit some things?
Screenshot_10.png (26.3 KB)

Futhermore when i change the download file to for example mailmerge.docx it works and the file is ok.
How is it possible that i need to do that? because it’s not that convenient to do that everytime.

@niek.moor

I’m using 2.25.1 64-bit for windows 10. I will double check with the latest version.

For passing authentication token in the postman, please use the authorization tab and to run API use Send and Download option instead of Send. Please check the following screenshots for details. Hopefully it will help you to test the API in the postman.

Hi, Using postman im able to get the merged file back so that seems to work!
Right now the response file is called ‘result’.
Is it possible to specify the outcome name like i was doing in cURL with git bash (which still doesn’t work), or does that normally work but just not in postman?
When making a call from my program i would like to save a file e.g. in a file server where i can specify the location and name of the file.
But i don’t know if both requirements are a problem or not.

@niek.moor

I have upgraded my git bash version to 2.28.0 and ExecuteMailMergeOnline API method is working as expected in it.

I doubt there is some issue with your JWT token used in the cURL command. Please run ExecuteMailMergeOnline API method in API Explorer, copy the cURL command from the API method, replace “-d {“Template”:{},“Data”:{}}” with “-F “Template=@C:/Test/SampleMailMergeTemplate.docx”
-F “Data=@C:/Test/SampleMailMergeTemplateData.txt”
–output C:/Test/MailMergeOnline.docx” and run it in the git bash. Hopefully it will resolve the issue.

@niek.moor

Can you please share some details? On which platform are you using Aspose.Words REST API? Because we have SDKs for different platforms to use Aspose.Words Cloud API directly in your programming language. We can share a working example with you.

Thanks! I manage to get the Git Bash command to work using this method!

1 Like

I’m not looking to work with the SDK, but with the API Call, the cURL method.
At the end i would like to select a template and data from my storage, probably an online database and send these 2 files with the call to your service. Then I would like to recieve the merged file and save it again in our database/server.
The program will be written in Java and from there we want to make the API call.

1 Like

@niek.moor

Good to know that you managed to cURL issue at your end. So it means you were getting the issue because of your expired token. Please note JWT Token has 24 hours lifetime and it expires after it. So if you are calling the API yourself via some rest client then you need to keep this in mind and regenerate the JWT token accordingly. However, we have taken care of this in Aspose Cloud SDKs.

curl -X POST "https://api.aspose.cloud/connect/token" 
-d "grant_type=client_credentials&client_id=[APP_SID]&client_secret=[APP_Key]" 
-H "Content-Type: application/x-www-form-urlencoded" 
-H "Accept: application/json"

Hi, I know about the token and that it can expire, but that wasn’t my problem.
I’ve compared the 2 cURL calls with each other and found out that i kept the <> markers in the call where the token is located. after removing them it worked :slight_smile:

1 Like

I don’t know if i’ts possible, but can you guys maybe give me a working example of making an API call in java? So not using the SDK but with the cURL method? Would be much appreciated!

@niek.moor

Sure, I will try to create a working sample and share with you asap.

Thankyou very much! i’m looking forward to your example :slight_smile:

1 Like

@niek.moor

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.

@niek.moor

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.io.File;
import java.io.IOException;
import java.io.InputStream;
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 https://dashboard.aspose.cloud/
	static String App_Key = "xxxxxxxxxxxxxxxxxxxxxxxxxxx";
	static String App_SID = "xxxxxx-xxxx-xxxx-xxxxx-xxxxxxx";

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

		MailMergeOnline();

		System.out.println("Completed.....");
	}

	public static void MailMergeOnline() throws IOException {
		// Get JWT Token
		String access_token = JWTAuth(App_Key, App_SID);
		
		OkHttpClient client = new OkHttpClient();
		String url = "https://api.aspose.cloud/v4.0/words/MailMerge?withRegions=false&documentFileName=template";
		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))
				.build();
		Request request = new Request.Builder().url(url).header("Content-Type", "multipart/form-data")
				.addHeader("Accept", "application/octet-stream")
				.addHeader("Authorization",
						"Bearer "+access_token)
				.put(body).build();

		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 = "https://api.aspose.cloud/connect/token";

		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.

@niek.moor

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")))
		.build();

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

@niek.moor

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