SOLVED

Bulk Import C# RestSharp

Go to solution
Highlighted

Bulk Import C# RestSharp

I'm exploring Bulk Import via the REST API and can successfully post an import through Postman. When I try to use the code snippet from Postman, I get the following error:

"{\"requestId\":\"1b0d#16875fb3c42\",\"success\":false,\"errors\":[{\"code\":\"1003\",\"message\":\"Empty file\"}]}"

The code is below:

if (File.Exists(Path.Combine(folderName, fileName)))

{

var client = new RestClient(host + "/bulk/v1/leads.json?format=csv&access_token=" + getToken());

var request = new RestRequest(Method.POST);

request.AddHeader("cache-control", "no-cache");

request.AddHeader("Content-Type", "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW");

//C:\\MarketoBulkImport\\import.csv is hard-coded below, but it is the same file path as what's being checked in the above File.Exists

request.AddParameter("multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW", "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"file\"; filename=\"C:\\MarketoBulkImport\\import.csv\"\r\nContent-Type: text/csv\r\n\r\n\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--", ParameterType.RequestBody);

IRestResponse response = client.Execute(request);

return response.ToString();

}

else {

return "file doesn't exists";

}

1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted
Level 10 - Community Moderator

Re: Bulk Import C# RestSharp

Based on the RestSharp docs, you're mixing together conflicting semantics.

When you AddHeader("Content-Type"), you don't add the boundary string, that's up to the library. Just set the MIME type.

You don't need to AddParameter() if you're using AddFile(). Make sure the MIME segment name is "file" as it is in the initial payload, not "import".

View solution in original post

9 REPLIES 9
Highlighted
Level 10 - Community Moderator

Re: Bulk Import C# RestSharp

Please highlight your code with the Advanced Editor's syntax highlighter so it's readable. Then we'll continue.

https://s3.amazonaws.com/blog-images-teknkl-com/syntax_highlighter.gif

Highlighted

Re: Bulk Import C# RestSharp

The code has been updated.

Highlighted
Level 10 - Community Moderator

Re: Bulk Import C# RestSharp

The file in the payload is indeed empty. You aren't reading the CSV from disk at any point.

Highlighted

Re: Bulk Import C# RestSharp

Even when I update the code with line 9, I still get the same error.

Highlighted
Level 10 - Community Moderator

Re: Bulk Import C# RestSharp

Could you go back and restore the code in the original post, then add your updated code in a reply?

(Hundreds of people read these threads and it's going to be impossible to follow otherwise.)

Highlighted

Re: Bulk Import C# RestSharp

I still get the same error when I try this

if (File.Exists(Path.Combine(folderName, fileName)))

{

var client = new RestClient(host);

var request = new RestRequest("/bulk/v1/leads.json?format=csv&access_token=" + getToken(), Method.POST);

request.AlwaysMultipartFormData = true;

request.AddHeader("cache-control", "no-cache");

request.AddHeader("Content-Type", "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW");

request.AddParameter("multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW", "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"file\"; filename=import.csv\r\nContent-Type: text/csv\r\n\r\n\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--", ParameterType.RequestBody);

request.AddFile("import", @"C:\MarketoBulkImport\import.csv");

IRestResponse response = client.Execute(request);

return response.ToString();

}

else

{

return "file doesn't exists";

}

Highlighted
Level 10 - Community Moderator

Re: Bulk Import C# RestSharp

Based on the RestSharp docs, you're mixing together conflicting semantics.

When you AddHeader("Content-Type"), you don't add the boundary string, that's up to the library. Just set the MIME type.

You don't need to AddParameter() if you're using AddFile(). Make sure the MIME segment name is "file" as it is in the initial payload, not "import".

View solution in original post

Highlighted

Re: Bulk Import C# RestSharp

That did it. Thank you for your help. Below is the working code snippet

if (File.Exists(Path.Combine(folderName, fileName)))

{

//host of your marketo instance, https://XXX-XXX-XXX.mktorest.com

var client = new RestClient(host);

var request = new RestRequest("/bulk/v1/leads.json?format=csv&access_token=" + getToken(), Method.POST);

request.AlwaysMultipartFormData = true;

request.AddHeader("cache-control", "no-cache");

request.AddHeader("Content-Type", "multipart/form-data;");

request.AddFile("file", Path.Combine(folderName, fileName));

IRestResponse response = client.Execute(request);

return response.ToString();

}

else

{

return "file doesn't exists";

}

Highlighted
Level 10 - Community Moderator

Re: Bulk Import C# RestSharp

POSTs aren't cached btw.