Bulk Import C# RestSharp | Community
Skip to main content
Level 4
Jan 22, 2019 2:47 PM UTC
Solved

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

}

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
SanfordWhiteman
SanfordWhitemanAccepted solution
Level 10
Jan 22, 2019 9:36 PM UTC

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

9 replies

SanfordWhiteman
Level 10
Jan 22, 2019 7:55 PM UTC

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

Translate
Level 4
Jan 22, 2019 8:51 PM UTC

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

Translate
SanfordWhiteman
Level 10
Jan 22, 2019 8:58 PM UTC

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

Translate
SanfordWhiteman
Level 10
Jan 22, 2019 6:28 PM UTC

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

Translate
Level 4
Jan 22, 2019 7:23 PM UTC

The code has been updated.

Translate