9 Replies Latest reply on Jan 22, 2019 2:06 PM by Sanford Whiteman

    Bulk Import C# RestSharp

    Jason Saunders

      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";
                  }
      
        • Re: Bulk Import C# RestSharp
          Sanford Whiteman

          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

          • Re: Bulk Import C# RestSharp
            Sanford Whiteman

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

              • Re: Bulk Import C# RestSharp
                Jason Saunders

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

                  • Re: Bulk Import C# RestSharp
                    Sanford Whiteman

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

                      • Re: Bulk Import C# RestSharp
                        Jason Saunders

                        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";
                                    }
                        
                          • Re: Bulk Import C# RestSharp
                            Sanford Whiteman

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

                              • Re: Bulk Import C# RestSharp
                                Jason Saunders

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