php post request

Hi all,

i would like to send a post request to a php server. I need a very simple request pushing some text data. I really do not know where to start with monkey C and toybox but i noticed that there is a module toybox.communication which could be the start point. Is out there any simple example???

Thank you

  • You can see the basics in the WebRequest sample in the SDK, and see options in the API doc for Communications and specifically makeWebRequest.

  • yes thank you very much! I was just reading that. I am thinking to develop a simple php page which receive simple data with $_POST and then decode the data using json_decode

    The device will sent the json data using the following 

    Communications.makeWebRequest(url, params, options, responseCallback);

    and taking care to correct fillup the url, params, options and the response call back....
    do you think it is the correct way or am i far away? :-D

  • Look at HTTP_REQUEST_METHOD_POST.  Also, note that unless your server runs on the phone, you heed to use HTTPS with a certificate Garmin likes.

  • ok! unfortunately i get a compile error which is almost not documented. The error is the following:

    ERROR: fenix5xplus: D:\750 Visual Studio Code\DrakeTowLogger\DrakeTowLogger\source\DrakeTowLoggerView.mc:61,7: Invalid '$.Toybox.Lang.Method(responseCode as Any) as Void' passed as parameter 4 of type 'PolyType<($.Toybox.Lang.Method(responseCode as $.Toybox.Lang.Number, data as Null or $.Toybox.Lang.Dictionary or $.Toybox.Lang.String or $.Toybox.PersistedContent.Iterator) as Void) or ($.Toybox.Lang.Method(responseCode as $.Toybox.Lang.Number, data as Null or $.Toybox.Lang.Dictionary or $.Toybox.Lang.String or $.Toybox.PersistedContent.Iterator, context as $.Toybox.Lang.Object) as Void)>'.

    the callback and the the test function to send the data is the following:

       // set up the response callback function
           function onReceive(responseCode) as Void
        {
            if (responseCode == 200)
                {
                    System.println("Request Successful");                   // print success
                }
                    else
                        {
                            System.println("Response: " + responseCode);            // print response code
                        }

        }

        // SEND THE WEB POST
        function SendWebTry()
            {

           var url = "">drakes.altervista.org/.../index.php";                         // set the url

           var params = {                                              // set the parameters
                  "definedParams" => "123456789abcdefg"
           };

           var options = {                                             // set the options
               :method => Communications.HTTP_REQUEST_METHOD_POST,      // set HTTP method
               :headers => {                                           // set headers
                       "Content-Type" => Communications.REQUEST_CONTENT_TYPE_URL_ENCODED},
                                                                       // set response type
               :responseType => Communications.HTTP_RESPONSE_CONTENT_TYPE_URL_ENCODED
           };

           var responseCallback = method(:onReceive);                  // set responseCallback to
                                                                       // onReceive() method
                System.println("try a data send");                                                                  
           // Make the Communications.makeWebRequest() call
           Communications.makeWebRequest(url, params, options, responseCallback);
            }
  • Sorry it is late and i do really try to setup a minimum working code! I apologize...

  • Try something like this:

    typedef webData as Null or $.Toybox.Lang.Dictionary or $.Toybox.Lang.String or $.Toybox.PersistedContent.Iterator; // SDK 6.4+
    
    public function onReceive(response as Number, data as webData) as Void {

  • It works!! I did not get the typedef from the api documentation

    great