Uploading CSV to contactList with Java resulting in 401 :[no body]

Hi I am creating a service to upload csv of contacts to a contact list ,

public String uploadContactsCSV(){

 try{
        final RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        headers.add(HttpHeaders.AUTHORIZATION,"Bearer " +genesysAuth.getToken());
        headers.setContentType (MediaType.MULTIPART_FORM_DATA);

        LinkedMultiValueMap<String, Object> body = new LinkedMultiValueMap<>();

       // byte[] csv = CSVCLwriter.CSVwriter();

        InputStream myInputStreamDocument = new ByteArrayInputStream(CSVCLwriter.CSVwriter());
        body.add("id", "9e21a551-59bb-4e56-b2bc-2e0b65a0d071");
        body.add("file", new MultipartInputStreamFileResource(myInputStreamDocument, "testingCSV"));
        body.add("fileType", "contactlist");
        body.add("contact-id-name", "CHD_CUST_EXTR_ID");

        System.out.println(body);

        HttpEntity<LinkedMultiValueMap<String, Object>> requestEntity = new HttpEntity<>(body, headers);
        System.out.println(requestEntity);
        ResponseEntity<Object> response = restTemplate.exchange("https://apps.mypurecloud.com/uploads/v2/contactlist", HttpMethod.POST, requestEntity, Object.class);


        System.out.println(response.getStatusCode());
    }catch (Exception e){
        e.printStackTrace();
        return e.getMessage();

    }
    return "testing....contacts added " ;

}

-------------------------------------------------------------------------------------------------------------------------------------------------------
<{id=[9e21a551-59bb-4e56-b2bc-2e0b65a0d071], file=[InputStream resource [resource loaded through InputStream]], fileType=[contactlist], contact-id-name=[CHD_CUST_EXTR_ID]},[Authorization:"Bearer HANDLE AUTH TOKENS AS PASSWORDS", Content-Type:"multipart/form-data"]>
org.springframework.web.client.HttpClientErrorException$Unauthorized: 401 : [no body]
at org.springframework.web.client.HttpClientErrorException.create(HttpClientErrorException.java:105)
at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:168)
at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:122)
at org.springframework.web.client.ResponseErrorHandler.handleError(ResponseErrorHandler.java:63)
at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:819)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:777)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:711)
at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:602)
----------------------------------------------------------------------------------------------------------------------------------------------------
I found this forum response and decided to follow it but still cant get to upload the csv to the list I created Bad request Upload Contact Lists

I know 401 is unauthorized but I am authorized to created list and to add contact to a list but why is happening when I am trying to upload the csv

I've edited out the access token from your post, but please be sure to handle auth tokens with the same level of security used for handling passwords.

Can you provide the correlation ID from the response header?

Hi tim, thanks for handling that , forgot I was printing out that too. unfortunatelly I am not getting correlationID, im just getting a solid 401 : [no body] as message

this is the config I use to get the access token
responseEntity = new RestTemplate()
.exchange("https://login.usw2.pure.cloud/oauth/token?grant_type=client_credentials", HttpMethod.POST,
httpEntity, Object.class);

since I am using it for the create contactList service,

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

do you think this has to be with the issue , since the creating contact list is working fine

I'm not familiar with the request library you're using, but from the docs it looks like you should be able to access the headers on the response object via response.getHeaders(). There should be a header named inin-correlation-id. If you're not getting a correlation ID on the response, you'll need to open a case with Genesys Cloud Care to troubleshoot further.

I had to change the uri from : (" https://apps.mypurecloud.com/uploads/v2/contactlist " to :https://apps.usw2.pure.cloud/uploads/v2/contactlist

this is the correlation id I got correlationId=55318c18-fd1c-42a6-8141-f8142ab949f1

Are you getting a different response code and error body now? I'm seeing in the logs that the request was accepted, but processing failed. The error code is IMPORT_ENTRY_DOES_NOT_ALIGN_WITH_HEADERS and there's a more specific message that may or may not be in a response: The headers and values in the file do not match: Headers' size: <49>, Values' length: <48>. Make sure that the file you're importing has exactly the columns that have been configured for the contact list; the error message sounds like your CSV is missing a column.

1 Like

Thanks so much Tim, this really cleared my mind , so what happened is that there was a property missing on the object I am adding as contact , this is the new correlation id {correlationId=b12f43d6-a487-4a79-8f78-63ab7f759f37 ,

Are you getting an error? The logging for that correlation ID looks like it completed successfully.

Good morning Tim, No I am not getting more errors , I am able to add upload the file as it should.

I have a question, in case I have an error while uploading how do I noticed it ? or in that case I should provide the correlation id and rise an genesys incident ?

Your app should check the HTTP status code from the response to ensure it's successful. If it is unsuccessful, be sure to log the HTTP status code, response body, and the correlation ID for troubleshooting. We can handle some stuff on the forum, but all 5xx responses and suspected bugs must go to Care.

1 Like

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