Search code examples
c#phpapicurlpushbullet

How can I implement curl (Pushbullet API) into my program?


How can I implement the curl commands given on the official Pushbullet API site into my C# program?

Example:

curl --header 'Access-Token: <your_access_token_here>' \
https://api.pushbullet.com/v2/users/me

Can I somehow directly write that code into my C# program or do I have do use php?


Solution

  • you can use HttpClient

    Here is my simple code snippet to make get/post requests using httpClient

    public async Task<T> MakeHttpClientRequestASync<T>(string requestUrl, string authenticationToken,
                    Dictionary<string, string> requestContent, HttpMethod verb, Action<Exception> error)
    {
         var httpClient = new HttpClient();
        //// add access token to AuthenticationHeader
         httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Access-Token",authenticationToken);
         HttpResponseMessage response;
    
         var returnVal = default(T);
    
         try
          {
           if (verb == HttpMethod.Post)
            {
             response = await httpClient.PostAsync(requestUrl, new FormUrlEncodedContent(requestContent));
            }
            else
            {
               response = await httpClient.GetAsync(requestUrl);
            }
    
            var resultString = await response.Content.ReadAsStringAsync();
            //// DeserializeObject using Json.net
            returnVal = JsonConvert.DeserializeObject<T>(resultString);
           }
           catch (Exception ex)
           {
             error(ex);
           }
    
        return returnVal;
    }
    

    and call it this way:

    MakeHttpClientRequestASync<T>("https://api.pushbullet.com/v2/users/me","your auth token",null, HttpMethod.Get,(errorAction)=>
                            {
                                // do something}
                            });