B4J Question How to "translate" this JAVA code to B4J?

hcm

Member
Licensed User
Longtime User
Hi all,
I wrote an WebApp which runs on a server and needs to upload user data and PDF files
to the Zoho CRM platform from time to time.
While I successfully implemented the Zoho API to create new client records, I am struggling
for a while now to upload related PDF files.
The Zoho JAVA example for uploads looks like this:

ZOHO JAVA example:
package com.zoho.crm.api.sample.restapi.filesandattachments;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import javax.net.ssl.SSLContext;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.ByteArrayBody;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
public class UploadanAttachment
{
    public static void main(String[] args)
    {
        try
        {
            HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
            SSLContext sslContext = SSLContext.getDefault();
            SSLConnectionSocketFactory sslConnectionSocketFactory = new SSLConnectionSocketFactory(sslContext, NoopHostnameVerifier.INSTANCE);
            CloseableHttpClient httpclient = httpClientBuilder.setSSLSocketFactory(sslConnectionSocketFactory).build();
            URIBuilder uriBuilder = new URIBuilder("https://www.zohoapis.eu/crm/v2/Leads/34770617711001/Attachments");
            HttpUriRequest requestObj = new HttpPost(uriBuilder.build());
            HttpEntityEnclosingRequestBase requestBase = (HttpEntityEnclosingRequestBase) requestObj;
            requestObj.addHeader("Authorization", "Zoho-oauthtoken 1000.xxxxxxx.xxxxxxx");
            MultipartEntityBuilder multipartEntity = MultipartEntityBuilder.create();
            java.io.File file = new java.io.File("/Users/username/Desktop/zoho.jpeg");
            @SuppressWarnings("resource")
            InputStream stream = new FileInputStream(file);
            byte[] buffer = new byte[8192];
            ByteArrayOutputStream output = new ByteArrayOutputStream();
            int bytesRead;
            while ((bytesRead = stream.read(buffer)) != -1)
            {
                output.write(buffer, 0, bytesRead);
            }
            multipartEntity.addPart("file", new ByteArrayBody(output.toByteArray(), file.getName()));
            requestBase.setEntity(multipartEntity.build());
            HttpResponse response = httpclient.execute(requestObj);
            HttpEntity responseEntity = response.getEntity();
            Object responseObject = EntityUtils.toString(responseEntity);
            String responseString = responseObject.toString();
            System.out.println(responseString);
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
    }
}

I tried to translate this to B4j, but I seem to miss something, as ZOHO always answers with "invalid request".
My header part with the Access token works well in the other modules.
This is how I tried to translate it, but I don't have any JAVA experience at all, so if there is a JAVA expert who
could give me a hint I would be really happy:

My code:
        Dim endpointURL As String ="https://www.zohoapis.eu/crm/v2/Deals/" & Deal_Id & "/Attachments"
        
        Dim ok As HttpJob
        ok.Initialize("",Me)
        
        Dim fd As MultipartFileData
        fd.Initialize
        fd.KeyName = "file"
        fd.Dir = File.DirApp &"/temp/"
        fd.FileName = Filename
        fd.ContentType = "application/octet-stream" 'usually it is PDF files which get uploaded 
        
        ok.PostMultipart(endpointURL,Null, Array(fd))
        ok.GetRequest.SetHeader("Authorization","Zoho-oauthtoken " & Access_Token)
        ok.GetRequest.SetContentType("multipart/mixed; boundary=---------------------------1461124740692")
        ok.GetRequest.SetContentEncoding("UTF8")
        
        Wait For (ok) Jobdone(ok As HttpJob)
        If ok.Success Then
            Log("File upload success")
            Log(ok.GetString)
        Else
            Log("File upload failed")
            Log(ok.ErrorMessage)
        End If
        ok.Release

Thanks a lot,
Chris
 
Solution
Why are you using
ok.GetRequest.SetContentType("multipart/mixed; boundary=---------------------------1461124740692")
?
The method PostMultipart will take care of the boundaries. You can look at the source of OKHttpUtils2 here:

OliverA

Expert
Licensed User
Longtime User
Why are you using
ok.GetRequest.SetContentType("multipart/mixed; boundary=---------------------------1461124740692")
?
The method PostMultipart will take care of the boundaries. You can look at the source of OKHttpUtils2 here:
 
Upvote 2
Solution

hcm

Member
Licensed User
Longtime User
Hi Oliver,
thanks a lot for the fast support ...
deleting the full line (ok.GetRequest.SetContentType("multipart/mixed; boundary=---------------------------1461124740692") solved the problem!
 
Upvote 0
Top