question

maritza-rico1133 avatar image
maritza-rico1133 asked Byrne Reese edited

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);
    }
}
rest api
1 |3000

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 Deactivated 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 |3000

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

vyshakhbabji avatar image
vyshakhbabji Deactivated 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 |3000

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 Anirban edited

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 |3000

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

Developer sandbox tools

Using the RingCentral Phone for Desktop, you can dial or receive test calls, send and receive test SMS or Fax messages in your sandbox environment.

Download RingCentral Phone for Desktop:

Tip: switch to the "sandbox mode" before logging in the app:

  • On MacOS: press "fn + command + f2" keys
  • On Windows: press "Ctrl + F2" keys