question

maritza-rico1133 avatar image
maritza-rico1133 asked ·

Send Fax doesn't work - I get the error: "Unsupported Media Type"

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);
    }
}
General Topicfaxjava415-error
1 |1000 characters needed characters left characters exceeded

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

John Wang avatar image
John Wang answered ·
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...
1 |1000 characters needed characters left characters exceeded

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

vb avatar image
vb answered ·
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
1 |1000 characters needed characters left characters exceeded

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

Anirban avatar image
Anirban answered ·

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

1 |1000 characters needed characters left characters exceeded

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

Work together.
From anywhere.

Team messaging, video meetings
and phone - all in one app.


Get the new RingCentral app