简体   繁体   中英

Curl command to C# web api request

I have following CURL command which I would like to translte into c# webapi call.

curl -H "Content-Type: application/x-www-form-urlencoded" \
-H "client_id: YOUR-CLIENT-ID" \
-H "client_secret: YOUR-CLIENT-SECRET" \
-d "mailbox_id=YOUR-MAILBOX-ID" \
--data-urlencode email@/path/to/email/contents.eml \
"https://api.abc.com/v2/add"

The part where i need help is how to add mailbox id and data as url encoded. also it is picking up email from disk. Can I just add byte array ?

Here is my example of c# code. I only need to add mailid and email content in there.

  public static string WebRequestWithByte(byte[] postData)
    {
        var url = @"https://api.abv.com/v2/add";
        var clientId = "wewew";
        var clientSecret = "df58ffed4bc0bc41";


        string ret = string.Empty;

        StreamWriter requestWriter;

        var webRequest = System.Net.WebRequest.Create(url) as HttpWebRequest;
        if (webRequest != null)
        {
            webRequest.Headers.Add("client_id", clientId);
            webRequest.Headers.Add("client_secret", clientSecret);
            webRequest.Method = "POST";
            webRequest.ServicePoint.Expect100Continue = false;
            webRequest.Timeout = 20000;
            webRequest.ContentType = "application/x-www-form-urlencoded";
            //POST the data.
            using (requestWriter = new StreamWriter(webRequest.GetRequestStream()))
            {
                requestWriter.Write(payloadStr);
            }
        }

You can use the below code. I suppose you want to make a Post call. You can add data into Dictionary and then convert it to byte array.

-d and --data are same.

This code is just for your reference, you can modify it accordingly.

private static String Post(String url,
            int timeout)
    {

        HttpWebRequest req = WebRequest.Create(url) as HttpWebRequest;
        req.Method = "POST";
        if (timeout != 0)
            req.Timeout = timeout;


        req.Headers.Set("client_id", "client_id");
        req.Headers.Set("client_secret", "client_secret");

        Dictionary<String, String> data = new Dictionary<String, String>();
        data.Add("mailbox_id", "mailbox_id");

        byte[] rawData = Encoding.UTF8.GetBytes(Encode(data));
        req.ContentType = "application/x-www-form-urlencoded";
        req.ContentLength = rawData.Length;

        String ret = null;
        using (Stream s = req.GetRequestStream())
        {
            s.Write(rawData, 0, rawData.Length);
            using (HttpWebResponse res = req.GetResponse() as HttpWebResponse)
            {
                using (Stream s2 = res.GetResponseStream())
                {
                    using (StreamReader r = new StreamReader(s2, Encoding.UTF8))
                    {
                        ret = r.ReadToEnd();
                    }
                }
            }
        }
        return ret;
    }
    public static String Encode(Dictionary<String, String> data)
    {
        StringBuilder s = new StringBuilder();
        foreach (KeyValuePair<String, String> o in data)
        {
            s.AppendFormat("{0}={1}&", o.Key, HttpUtility.UrlEncode(o.Value));
        }

        char[] trim = { '&' };
        String ret = s.ToString().TrimEnd(trim);
        return ret;
    }

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