2

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?

CodingGorilla
  • 19,612
  • 4
  • 45
  • 65
Mr_Grennn7
  • 109
  • 1
  • 12
  • 2
    possible duplicate of [making-a-curl-call in C#](http://stackoverflow.com/questions/7929013/making-a-curl-call-in-c-sharp) – esiprogrammer Mar 23 '16 at 13:05

1 Answers1

0

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}
                        });
esiprogrammer
  • 1,438
  • 1
  • 17
  • 22
  • I tryed using exactly your code but i 3 errors: is it efaultRequestHeaders or DefaultRequestHeaders also the AuthenticationHeaderValue is underlined and the JsonConvert is underlined. Do i have do include anything other than using System.Net.Http;? I am a noop so sorry if this is a stupid question. – Mr_Grennn7 Mar 23 '16 at 13:45
  • sorry it was a typing mistake, i edited my answer. yes you need to add `using System.Net.Http; using System.Net.Http.Headers; using Newtonsoft.Json;` – esiprogrammer Mar 23 '16 at 13:50
  • You need to install Json.net, [Json.net](https://www.nuget.org/packages/Newtonsoft.Json/) , but alternatively you can just return string response – esiprogrammer Mar 23 '16 at 13:51
  • ok i have install Json.net in included it but the line where I call it still doesnt work the MakeHttpClientRequestASync(...) is underlined in green it says that i should consider the Await-operators. It also says the T namespace couldn´t be found – Mr_Grennn7 Mar 23 '16 at 14:04
  • yes because it's an sync method, also this method is Generic, you need to implement your response object and replace it with 'T' . u need to learn more about `async and generic methods` – esiprogrammer Mar 23 '16 at 14:19
  • but as i said, as an alternative you can change response type to `string` just replace `Task` to `Task` and directly return `resultString` object – esiprogrammer Mar 23 '16 at 14:24