Skip to main content

I use java to send a fax via the RingCentral Fax API. However I got HTTP 415 error code. I really have no idea how to make it work.

public void sendFax(String body, String...toList) {
    CloseableHttpClient httpClient = HttpClients.createDefault();
    HttpPost httpPost = new HttpPost(baseUrl + URI_FAX);
    httpPost.addHeader("Content-Type", "multipart/mixed; boundary=Boundary_1_6506429161_101");
    httpPost.addHeader("Authorization", "Bearer " + rcoAuthResp.getAccess_token());

    try {
        List < RCPhone > list = new ArrayList < > ();
        for (String to: toList) {
            list.add(RCPhone.builder().phoneNumber(to).build());
        }
        String requestBody = mapper.writeValueAsString(RCFaxPayload.builder().to(list).faxResolution("High").build());

        FormBodyPart bodyPart1 = FormBodyPartBuilder.create()
            .setName("json")
            .setBody(new StringBody(requestBody, ContentType.APPLICATION_JSON))
            .build();
        bodyPart1.getHeader().removeFields("Content-Disposition");
        bodyPart1.getHeader().removeFields("Content-Transfer-Encoding");
        bodyPart1.getHeader().removeFields("Content-type");
        bodyPart1.addField("Content-type", "application/json");

        FormBodyPart bodyPart2 = FormBodyPartBuilder.create()
            .setName("body")
            .setBody(new StringBody(body, ContentType.TEXT_PLAIN))
            .build();
        bodyPart2.getHeader().removeFields("Content-Disposition");
        bodyPart2.getHeader().removeFields("Content-Transfer-Encoding");
        bodyPart2.getHeader().removeFields("Content-type");
        bodyPart2.addField("Content-type", "text/plain");

        HttpEntity entity = MultipartEntityBuilder
            .create()
            .setBoundary("Boundary_1_6506429161_101")
            .addPart(bodyPart1)
            .addPart(bodyPart2)
            .build();

        httpPost.setEntity(entity);
        httpPost.getEntity().writeTo(System.out);

        String response = processHttpResp(httpClient, httpPost);
        System.out.println(response);
    } catch (Exception e) {
        log.error(e.getMessage(), e);
    }
}
I haven't had time to examine your code in detail, but you can do the following:

Send a fax using the RC API Explorer to verify faxing is working.

Compare your output to the sample output shown here:

faxing text: https://developer.ringcentral.com/api-docs/latest/index.html#!#RefCreateFaxMessage

faxing files: http://ringcentral-sdk-ruby.readthedocs.org/en/latest/usage/messages/Fax-Messages/#http-request-exam...

Please have a look at this code.. https://github.com/vyshakhbabji/ringcentral-java/blob/master/src/utils/SendFax.java  This will help you send faxes. 

Also, you could make use of the unofficial JAVA SDK  for your development. If you have any concerns or questions do let me know and I can hep you out with the development

Before putting the implementation in code, the best practicei s to check if it is working in API. You need to log into API explorer here and test the ringcentral fax API to confirm if it's working for you. After that you can call it from you code.

415 http error means media type not supported, which means the content-type you are setting is not correct


Reply