1

I have seen How do I replace curl invoke with HttpClient object invoke or some similar? but it didn't answer my question. I have been given the below cURL command by our vendor, with some optional parameters, and instructions to use a CRON job to run it. It seems it would be more efficient to send the CSV my C# code is generating via HttpClient rather than dropping the CSV somewhere and going through the CRON job business. The C# I am writing will be part of an automation service we already have in place.

Here is the cURL:

curl -F "passkey=<PASS KEY>" -F "uploadFile=@<PATH_TO_YOUR_CSV_FILE>" https://td-setup.xxxxxx.com/<DOMAIN NAME>/csvupload

There is a loadAll parameter that I will want to use.

I have added Microsoft.Net.Http -Version 2.2.22 to my project via NuGet, and now find myself not sure what to do next. Sadly, this is all the further I've gotten:

       string baseAddress = String.Format("{0}/{1}/{2}", taskConfig.Upload.BaseUrl, domain.Username, taskConfig.Upload.UploadLocation);
        using (HttpClient client = new HttpClient)
        {

        }

I typically like to have demonstrated more of an effort before asking her help, but I'm over my head. How do I begin? Add the passkey? the loadAll parameter?

1 Answer 1

4

The curl -F option indicates POST data using the multipart/form-data content type as per RFC 2388.

So you need to create a MultipartFormDataContent object:

var formDataContent= new MultipartFormDataContent();

Attach your password string to it:

// passkey is the param name from the curl script
formDataContent.Add(new HttpStringContent("yourpasswordhere"), "passkey");

Attach your CSV file content to it:

byte[] csvBytes = File.ReadAllBytes("yourcsvfile.csv");
var csvContent = new ByteArrayContent(csvBytes);

// uploadFile is the param name specified in the curl script
formDataContent.Add(csvContent, "uploadFile", "filename.csv");

You don't explain how the loadAll parameter is used in curl but lets pretend it's a 'true'/'false' string parameter:

formDataContent.Add(new HttpStringContent("true"), "loadAll");

Finally post the form:

string url = "https://td-setup.xxxxxx.com/<DOMAIN NAME>/csvupload";
var client = new HttpClient();
await client.PostAsync(url, formDataContent);
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.