Upload Contactlist API

Hi,

I'm trying to automate the upload contact list process. I try to use the /uploads/v2/contactlist API but without success. Follow how I'm calling the API:

$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Content-Type", "application/json")
$headers.Add("Content-Type", "multipart/form-data; boundary=----WebKitFormBoundary")
$body = '{"id":"f40f5640-3135-4d2f-bd72-b8f2a5a1dc3e","file":"[path and file].csv","filetype":"contactlist","contact-id-name":"DEBTOR"}'
$response = Invoke-RestMethod 'https://apps.mypurecloud.com/uploads/v2/contactlist' -Method 'POST' -Headers $headers -Body $body

I tried to use the WebRequest method too but every call returns 400 Bad Request without any other information.

$response = Invoke-WebRequest -Uri 'https://apps.mypurecloud.com/uploads/v2/contactlist' -Method 'POST' -Headers $headers -Body $body

Another way I tried was using the postman to test. Unfortunately without success too.

Can anyone give me a direction to upload a contact list?

Hi,

The answer here might be helpful in solving your problem.

It's worth noting that https://apps.mypurecloud.com/uploads/v2/contactlist isn't part of the API. You're most likely looking for /api/v2/outbound/contactlists/{contactListId}/contacts if you want to use the API.
Bear in mind that you'll have to authenticate before making your request, this article on authorization should be helpful.
The API Explorer would be the most useful tool available to craft your request.

To add to my answer, there's documentation here on uploading contact lists.
It appears that your body may be incorrect; The server is expecting multipart form-data, not JSON. You have the correct header but the body format isn't.

Thank you, Ronan! I think that you solve my issue! I'll change the body to multipart form-data and let you know!

Now it's working! Thanks for your help Ronan!

1 Like

Hi,

I'm still with some problems. It's very strange due that in Postman runs perfectly but when I get the Powershell code generated in Postman and run as a script I got the 400 status. Follow the code in Powershell. Where am I doing wrong?
PS.: I already try: without the boundary in the headers; using ContentType = 'text/plain'; using ContentType = "application/octet-stream".

function ImportContactList($ContactListID,$FileName) {
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Authorization", "Bearer "+$token)

add-type -AssemblyName System.Net.Http
$multipartContent = [System.Net.Http.MultipartFormDataContent]::new()
$stringHeader = [System.Net.Http.Headers.ContentDispositionHeaderValue]::new("form-data")
$stringHeader.Name = "id"
$stringContent = [System.Net.Http.StringContent]::new($ContactListID.id)
$stringContent.Headers.ContentDisposition = $stringHeader
$multipartContent.Add($stringContent)

$multipartFile = $FileName
$FileStream = [System.IO.FileStream]::new($multipartFile, [System.IO.FileMode]::Open)
$fileHeader = [System.Net.Http.Headers.ContentDispositionHeaderValue]::new("form-data")
$fileHeader.Name = "file"
$fileHeader.FileName = Split-Path -leaf $FileName
$fileContent = [System.Net.Http.StreamContent]::new($FileStream)
$fileContent.Headers.ContentDisposition = $fileHeader
$multipartContent.Add($fileContent)

$stringHeader = [System.Net.Http.Headers.ContentDispositionHeaderValue]::new("form-data")
$stringHeader.Name = "fileType"
$stringContent = [System.Net.Http.StringContent]::new("contactlist")
$stringContent.Headers.ContentDisposition = $stringHeader
$multipartContent.Add($stringContent)

$stringHeader = [System.Net.Http.Headers.ContentDispositionHeaderValue]::new("form-data")
$stringHeader.Name = "contact-id-name"
$stringContent = [System.Net.Http.StringContent]::new("PLATFORM-DEBTOR-XLOCATOR")
$stringContent.Headers.ContentDisposition = $stringHeader
$multipartContent.Add($stringContent)
$headers.Add("Content-Type", "multipart/form-data; boundary="+$multipartContent.Headers.ContentType.Parameters.Value)

$body = $multipartContent
try{
$response = Invoke-RestMethod 'https://apps.mypurecloud.com/uploads/v2/contactlist' -Method 'POST' -Headers $headers -Body $body
$FileStream.Close()
}
catch{
$FileStream.Close()
}
}

I'm not personally familiar with building requests in powershell, but the 400 response is indicating that something is malformed in your request. Refer to the values used in the example Ronan linked above, as well as the multipart form data RFC that defines the format for such a request.

Thanks for your reply, Tim!
It is strange because when I run it on Postman, the upload is done. I got the code from Postman, but it didn't work.
I try to run in cURL too and here are the response:
2021-02-03 11:20:16 ::: Request curl -X POST -H 'Authorization: Bearer $token' -F 'id="19e73dac-9b87-44b6-a8e8-fb39bfa10826"' -F "file=@C:\FDR\LoadFiles\1-TST_PC_20210203_TestDaniel.csv" -F 'fileType="contactlist"' -F 'contact-id-name="PLATFORM-DEBTOR-XLOCATOR"' 'https://apps.mypurecloud.com/uploads/v2/contactlist'

2021-02-03 11:20:23 ::: Result {"correlationId":"171b137c-94df-4eb7-beb5-55e31c940b71"}

In this case, the API returns me the correlation ID, but the file was not uploaded.

The error associated with that correlation ID is "A ContactList with id $ContactListID could not be found". It appears that the literal value $ContactListID is being used as the ID for the contact list. Either your request is sending that value or your request is malformed in some way that's preventing the service from getting the ID you're trying to send.

Hi Tim,

I got the error here! I was sending this parameter on a wrong way.
Thanks for all the help Tim and Ronan!

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