Bad request Upload Contact Lists

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 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(), ERROR_SERVICE_API_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 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(), ERROR_SERVICE_API_PURECLOUD, e);
}

-> Error Bad Request 400

can you help me?

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.

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

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.

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.MULTIPART_FORM_DATA);

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!

1 Like

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