tags:

views:

29

answers:

1

I doubt anyone has specific experience related to this particular task, but maybe you can spot my problem. I'm trying to make a call to lithium (forum software) to place a vote in their poll, and their docs show this:

Example URL: http://community.lithium.com/community-name/restapi/vc/polls/id/15/votes/place

Query Arguments: poll.choice (required): - the choice to place the vote for. The choice is specified by a string of the form id/choice_id where choice_id is the id of the poll choice

Http Method: POST

So my code looks something like this:

Dim _Response As New XmlDocument    
Dim RestApiRoot As String = "http://example.com/community-name/restapi/vc/polls/id/6/votes/place"

APIRequest = WebRequest.Create(RestApiRoot)  
APIRequest.Method = "POST"    
APIRequest.Headers.Add("poll.choice", HttpContext.Current.Server.UrlEncode("id/" & _choiceID.ToString))

APIResponse = APIRequest.GetResponse()    
APIReader = New StreamReader(APIResponse.GetResponseStream())

_Response.LoadXml(APIReader.ReadToEnd())

APIResponse.Close()

I'm not able to successfully register a vote and they say it's because the poll.choice param is not appearing in the header, but if I step through debugging, I see it in the Header Keys/Items just fine.

Anyone have any clue what I might be doing wrong?

+1  A: 

I do exactly this with RestSharp, an open source REST framework. It works great with the Lithium REST API.

You're code will look something like this using RestSharp:

You'll create a class to look like the response from the Lithium API, in this case "Response". It will look like this (sorry, you'll have to translate this to VB.NET):

public class LithiumResponse
{
    public string status { get; set; }
    public string value { get; set; }
    public string message { get; set; } 
}

Now RestSharp will use that to capture the result like this:

// create the request
var request = new RestRequest();
request.Verb = Method.POST;
request.BaseUrl = "http://example.com/community-name";

// specify the action 
request.Action = "restapi/vc/polls/id/6/votes/place";

// add the parameters
request.AddParameter("poll.choice", "id/" + _choiceID.ToString());

// now create a RestClient to execute the request, 
// telling it to put the results in your "reponse" class
var client = new RestClient();
var lithiumresponse = client.Execute<LithiumResponse>(request);

// now you can check the status property of your class to 
// see if it was successful
if (lithiumresponse.status == "success")
    // you successfully placed a vote

I use RestSharp for a lot of interaction with the Lithium API and it makes it brain-dead simple. Pretty awesome library.

Ryan Farley