简体   繁体   中英

Equivalent curl request to c# HttpClient

How to make following curl request in c# HttepClient

curl -X POST “ https://api.knurld.io/v1/endpointAnalysis/file ” \\ -H "Authorization: $AUTHORIZATION" \\ -H "Developer-Id: $DEVELOPER_ID" \\ -H “multipart/form-data” \\ -F "filename=PATH_TO_FILE"

This Is the sync implemntation but I hope is what you need

    public void Send(string auth, string filePath, string developerId)
    {
        string payload = System.IO.File.ReadAllText(filePath);
        var content = new StringContent(payload);
        using (var client = new HttpClient())
        {
            client.DefaultRequestHeaders.Add("Authorization", auth);
            client.DefaultRequestHeaders.Add("Content-Type", "multipart/form-data");
            client.DefaultRequestHeaders.Add("Developer-Id", developerId);
            var result = client.PostAsync("https://api.knurld.io/v1/endpointAnalysis/file", content).Result;
            string resultContent = result.Content.ReadAsStringAsync().Result;
        }
    }

Kind Regards

For HttpClient:

async Task Send(string developerId, string pathToFile, string auth)
    {
        using (HttpClient c = new HttpClient())
        {
            c.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(auth);
            c.DefaultRequestHeaders.Add("Developer-Id", developerId);
            var multipartFormDataContent = new MultipartFormDataContent();
            using (Stream fileStream = new FileStream(pathToFile, FileMode.Open))
            {
                multipartFormDataContent.Add(new StreamContent(fileStream));
                HttpResponseMessage httpResponse = await c.PostAsync(@"https://api.knurld.io/v1/endpointAnalysis/file", multipartFormDataContent);
                string response = await httpResponse.Content.ReadAsStringAsync();
            }
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM