Bulk Import C# RestSharp | Community
Skip to main content
New Participant
January 22, 2019
Solved

Bulk Import C# RestSharp

  • January 22, 2019
  • 2 replies
  • 12105 views

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

}

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by SanfordWhiteman

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

}


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

2 replies

SanfordWhiteman
New Participant
January 22, 2019

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

New Participant
January 22, 2019

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

SanfordWhiteman
New Participant
January 22, 2019

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

SanfordWhiteman
New Participant
January 22, 2019

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

New Participant
January 22, 2019

The code has been updated.