Legacy Dev Forum Posts

 View Only

Sign Up

Bad request Upload Contact Lists

  • 1.  Bad request Upload Contact Lists

    Posted 06-05-2025 18:18

    DanielaZulueta | 2021-01-13 13:45:08 UTC | #1

    Hi, how can I upload a csv to a contactlist with java?

    try {

    final RestTemplate restTemplate = utils.createRestTemplate(); HttpHeaders headers = new HttpHeaders(); headers.add(AUTHORIZATION, "Bearer " + accessToken);

    MultiValueMap<String, Object> body = new LinkedMultiValueMap<>(); body.add("id", "3e4465f8"); body.add("file", sfos.read()); body.add("fileType", "contactlist"); body.add("contact-id-name", "PhoneId_1");

    HttpEntity requestEntity = new HttpEntity<>(body, headers);

    ResponseEntity<Object> response = restTemplate.exchange("https://apps.mypurecloud.com/uploads/v2/contactlist", HttpMethod.POST, requestEntity, Object.class);

    System.out.println(response);

    } catch (Exception e) { e.printStackTrace(); throw new ServiceException(this.getClass().getName(), ERRORSERVICEAPI_PURECLOUD, e); }

    -> Error Bad Request 400

    another way i try...

    public class UploadContact {

    private String id; private int file; private String fileType; @JsonProperty("contact-id-name") private String contactIdName; ...........

    try {

    final RestTemplate restTemplate = utils.createRestTemplate(); HttpHeaders headers = new HttpHeaders(); headers.add(AUTHORIZATION, "Bearer " + accessToken);

    UploadContact body = new UploadContact(); body.setId("3e4465f8"); body.setFile(sfos.read()); body.setFileType("contactlist"); body.setContactIdName("PhoneId_1");

    HttpEntity requestEntity = new HttpEntity<>(body, headers);

    ResponseEntity<Object> response = restTemplate.exchange("https://apps.mypurecloud.com/uploads/v2/contactlist", HttpMethod.POST, requestEntity, Object.class);

    System.out.println(response);

    } catch (Exception e) { e.printStackTrace(); throw new ServiceException(this.getClass().getName(), ERRORSERVICEAPI_PURECLOUD, e); }

    -> Error Bad Request 400

    can you help me?


    tim.smith | 2021-01-13 14:28:33 UTC | #2

    It could be several things causing the 400. What's the error message in the response body? It should contain more information to indicate what's wrong. Also, please provide the correlation ID from the response header.


    DanielaZulueta | 2021-01-13 19:50:58 UTC | #3

    -> The id of the contact list exite, by postman with that id I can upload a csv.

    _________________________________________________________________

    I am not getting a purecloud bad data error.

    The error that returns is:

    org.springframework.web.client.HttpClientErrorException: 400

    Do you have any example for java to upload csv?

    _________________________________________________________________

    You can go for the purecloud api, this example:

    ApiClient apiClient = ApiClient.Builder.standard() .withAccessToken(accessToken) .withBasePath("https://api.mypurecloud.com") .withProxy(proxy) .build();

    OutboundApi apiInstance = new OutboundApi();

    apiInstance.endpoint(body);

    _________________________________________________________________

    I'm trying to follow the example they have for javascript, but in java it always returns 400

    https://developer.usw2.pure.cloud/api/rest/v2/outbound/uploadcontactlists.html


    tim.smith | 2021-01-13 15:32:35 UTC | #4

    What is responseBody as a string? That's what should have information about why your request failed. Also, please gather the correlation ID from the response header and post it here. The stack trace isn't helpful here as that's a description of the call stack in your application. What we need to see here is the response from the API server.

    There is not a Java example that I'm aware of.


    DanielaZulueta | 2021-01-13 19:49:18 UTC | #5

    Problem solved. The correlative id you reference is the api response. That was not returned by the bad request 400.

    The problem was how to send the file.

    In the header it is necessary to send headers.setContentType (MediaType.MULTIPARTFORMDATA);

    It was necessary to create the following class

    public class MultipartInputStreamFileResource extends InputStreamResource {

    private final String filename;

    public MultipartInputStreamFileResource(InputStream inputStream, String filename) {

    super(inputStream); this.filename = filename; }

    @Override public String getFilename() { return this.filename; }

    @Override public long contentLength() throws IOException { return -1; // we do not want to generally read the whole stream into memory ... }

    }

    then...

    LinkedMultiValueMap<String, Object> body = new LinkedMultiValueMap<>(); InputStream myInputStreamDocument = new ByteArrayInputStream(byte[] (your csv));

    body.add("id", contactListId); body.add("file", new MultipartInputStreamFileResource(myInputStreamDocument, nameYourCsv)); body.add("fileType", "contactlist"); body.add("contact-id-name", "PhoneId_1");

    HttpEntity requestEntity = new HttpEntity<>(body, headers);

    . . .

    I leave this solution in case it works for someone! I hope they will add this service to the maven library soon!

    Thanks!


    system | 2021-02-13 19:48:05 UTC | #6

    This topic was automatically closed 31 days after the last reply. New replies are no longer allowed.


    This post was migrated from the old Developer Forum.

    ref: 9682