Send data via HTTP

Former Member
Former Member

Hello,

I have been trying a couple of things but still I cannot solve this problem.

I am developing quite a simple Application, which shall include sending data via HTTP requests.

There is one request to receive data and one to send. I can see that there is a connection but it sends empty files.

What do I need to do to send the heart rate with the HTTP-Post?

The code I use is the following

    function onSend(responseCode, stream) {
		Sys.println("OnSend " + stream);

		stream = model.getHeartRate();
		
			//check if request was successful
       if (responseCode == 200) {
       		
        Sys.println("Sending Successful");                  
      
   	 	} else {
         Sys.println("Response Send: " + responseCode);           
       		}

       		Ui.requestUpdate();

       }
       
    function sendData() {

 		var url = www.placeholder.com //I am testing with a local IP 
       	var params = stream;
	
				// define the HTTP methods and response type
       	var	options = {                                            
          	 	:method => Communications.HTTP_REQUEST_METHOD_POST,      // set HTTP method
           		:headers => {"Content-Type" => Communications.REQUEST_CONTENT_TYPE_JSON},                                                    
           		:responseType => Communications.HTTP_RESPONSE_CONTENT_TYPE_JSON // set response type
      	};

				// set responseCallback 
       var responseCallback = method(:onSend);                  
                                                                
                                                                
       	// make the Communications.makeWebRequest() call to send data
       	
       Communications.makeWebRequest(url, params, options, method(:onSend));

  	}

Thank you in advance for your help.

  • Former Member
    0 Former Member over 5 years ago in reply to jim_m_58

    yes, I know and usually there is also localhost, I just put that as a placeholder, sorry for the confusion!

    what my actual problem is, is that I dont know what I need to put in the code to actual send out the HR.

    The HTTP-Post request works but it just does not send the heart rate. Do you have a tip?

  • What kind of app?  Will you enable SENSOR_HEARTRATE?

    If so, Senor.Info.heartRate

  • Given your code snippet, I it seems that there is some misunderstanding about how the makeWebRequest API works. You are making a request with makeWebRequest, getting the response in onSend, and then overwriting the response with the result of model.getHeartRate().

    To clarify what is happening, your onSend method should probably be called something like onResponse or onResponseReceived.

    The data that you want to send needs to be supplied to the makeWebRequest call in some way. I'd expect your code to look something like this:

    function sendData() {
    
    	var url = www.placeholder.com //I am testing with a local IP 
    
        // this is the data that you want to send out
        var params = {
            "samples" => [ 1, 2, 3, 4 ]
        }
    	
      	var	options = {                                            
      	 	:method => Communications.HTTP_REQUEST_METHOD_POST,
       		:headers => {"Content-Type" => Communications.REQUEST_CONTENT_TYPE_JSON},                                                    
       		:responseType => Communications.HTTP_RESPONSE_CONTENT_TYPE_JSON
     	};
    
       	// make the Communications.makeWebRequest() call to send data
        Communications.makeWebRequest(url, params, options, method(:onResponse));
    }
    
    function onResponse(responseCode, responseData)
    {
        // responseCode is typically an HTTP response code
        // responseData is the HTTP response.
        
        // changes you make to either of these variables will not affect
        // any outoing message, these parameters are inputs only. the
        // request has already been sent. if you want to send additional
        // data, you should call makeWebRequest again.
    }

  • Former Member
    0 Former Member over 5 years ago in reply to Travis.ConnectIQ

    Thank you very very much for taking so much time to explain this to me! I think I understand my mistake now, this was very helpful.

    I will try to change it accordingly!