0

i've been following this Making a cURL call in C# for trying to make a request with the LiveChat API in curl and receive the result on my C# application,

the request should be filled as the following as explained in: http://developers.livechatinc.com/rest-api/#!introduction

curl "https://api.livechatinc.com/agents" \
    -u john.doe@mycompany.com:c14b85863755158d7aa5cc4ba17f61cb \
    -H X-API-Version:2 

This is what i did in C#:

static void Main(string[] args)
{
    RequestTest();
    Console.ReadKey();
}

private static async void RequestTest()
{
    var client = new HttpClient();

    // Create the HttpContent for the form to be posted.
    var requestContent = new FormUrlEncodedContent(new[] {new KeyValuePair<string, string>("myemail:myapikey", "X-API-Version:2"),});

    // Get the response.
    HttpResponseMessage response = await client.PostAsync(
        "https://api.livechatinc.com/agents",
        requestContent);

    // Get the response content.
    HttpContent responseContent = response.Content;

    // Get the stream of the content.
    using (var reader = new StreamReader(await responseContent.ReadAsStreamAsync()))
    {
        // Write the output.
        Console.WriteLine(await reader.ReadToEndAsync());
    }
}

The result seems to be allways the same "Cannot POST to /agents"

Community
  • 1
  • 1
EchO
  • 1,274
  • 1
  • 10
  • 17
  • I think you want to be doing a GET request. Not a POST. POST is used to create a new agent: http://developers.livechatinc.com/rest-api/#create-agent (and that requires you to send a JSON request payload defining the new agent to create). You want this: http://developers.livechatinc.com/rest-api/#get-single-agent – Oliver McPhee Jun 05 '15 at 10:40
  • @OliverMcPhee you're right but how can i add the "-u john.doe@mycompany.com:c14b85863755158d7aa5cc4ba17f61cb \ -H X-API-Version:2 " using a client.GetAsync method – EchO Jun 05 '15 at 10:54
  • Take a look at the answer to this thread: http://stackoverflow.com/questions/12022965/adding-http-headers-to-httpclient-asp-net-web-api – Oliver McPhee Jun 05 '15 at 11:00

1 Answers1

1

You're performing a POST operation here. That is reserved for creating a new agent and requires that you send in a JSON request payload. See here: developers.livechatinc.com/rest-api/#create-agent

What you want to do is a GET operation: developers.livechatinc.com/rest-api/#get-single-agent

Instead of using PostAsync you'll need to create an HttpRequestMessage, set the Method to GET, set your headers and then use SendAsync. See solution here: Adding Http Headers to HttpClient

Remember, for REST API:

POST = Create Operations,
GET = Read Operations,
PUT = Update Operations,
DELETE = Delete Operations
Community
  • 1
  • 1
Oliver McPhee
  • 1,010
  • 10
  • 18