Using POST method from Windows Phone

Hi,
I’m new developing with ElectricImp.
I try to make an simple App with visual studio c# for windows phone 8.0. This App simply send a request with led=“string” with an Http Request using POST method.
From an HTML page all function good, but when I try with this App, the agent does not seem to get anything.

The Visual C # code works for other sites and I can not understand why it does not work with the agent of electricimp.
I know this is not a site for visual c # developer but maybe someone already solved.
Thank you
Carlo

This is the agent code :

`function requestHandler(request, response) {
foreach(k, v in request.query) {
server.log(k +" = " + v);
}
try {
// check if the user sent led as a query parameter
if (“led” in request.query) {

    server.log("Ledstate: " + request.query.led);
    local ledState = request.query.led;

    // send "led" message to device, and send ledState as the data
    device.send("led", ledState); 
    server.log("led =" + ledState);
  }
//}
// send a response back saying everything was OK.
//server.log("led =" + ledState);
response.send(200, "OK * Ricevuto da App *");

} catch (ex) {
response.send(500, "Internal Server Error: " + ex);
}
}
// register the HTTP handler
http.onrequest(requestHandler);`

This is the Visual C# code

` private void Button_Click(object sender, RoutedEventArgs e)
{
string url = “https://agent.electricimp.com/XXXXXXXXXXX”;
Uri uri = new Uri(url, UriKind.Absolute);

        StringBuilder postData = new StringBuilder();
        postData.AppendFormat("{0}={1}", "led", HttpUtility.UrlEncode("string"));
        WebClient client = default(WebClient);
        client = new WebClient();
        client.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
        client.Headers[HttpRequestHeader.ContentLength] = postData.Length.ToString();

        client.UploadStringCompleted += client_UploadStringCompleted;
        client.UploadProgressChanged += client_UploadProgressChanged;

        client.UploadStringAsync(uri, "POST", postData.ToString()); 

}`

You’re sending a POST request with a body from your c# code, but are looking at the query parameters in your Squirrel code. You need to modify your agent to look at the body (and from the looks of your c# code, urldecode the body):

`function requestHandler(request, response) {
try {
local data = http.urldecode(request.body);
foreach(k, v in data) {
server.log(k +" = " + v);
}

//... actual handler goes here ...

} catch (ex) {
response.send(500, "INTERNAL AGENT ERROR - " + ex);
}
}`

Also - I wrapped your code blocks in < code > </ code > tags for easier reading :slight_smile:

Great Beardedinventor ,
You’ve Solved my problem ! :slight_smile:
Many many thanks
Carlo

Glad you got it working!

Also, as a .net developer in a past life, I’m always happy to see people using c# :slight_smile: