Help using makeWebRequest() please

I'd like to read in a GPX file using makeWebRequest().

I started with the webRequest sample app and got that to work.

I've created a GPX file for testing, placed in in a Google Drive, set sharing to anyone with the link, and verified with a browser that the link works to read the file.

I'm now trying to read that file using makeWebRequest() and I get a -400 error indicating "Response body data is invalid for the request type".

I've tried many combinations and variations in the options to the makeWebRequest() call. So far they all have returned -400.

Can someone provide a solution..... what combination of parameters to the makeWebRequest() call will let me read the file ?

Here's the code that makes the call:

//! Make the web request
private function makeRequest() as Void
{
_notify.invoke("Executing\nRequest");

var options =
{ :method => Communications.HTTP_REQUEST_METHOD_GET,
//:headers => { "Content-Type" => Communications.REQUEST_CONTENT_TYPE_JSON }, // URL_ENCODED },
:responseType => Communications.HTTP_RESPONSE_CONTENT_TYPE_JSON //TEXT_PLAIN // was TYPE_JSON
};


var reqStr = "">drive.google.com/.../view
System.println("request string = " + reqStr );

Communications.makeWebRequest( reqStr, {}, options, method(:onReceive) );

}

  • Ok, I just tried a test with a text file containing a single, short, line of text and still get a -400 error.

    I found an example on the forum from several years ago that apparently worked for Coleman, yet the developer was running into a -400 error. I copied the parameters that apparently worked there but still get the error.

    Can someone set me straight so I can read a single line of text from a file ?

    Thanks

    private function makeRequest() as Void
    {
    _notify.invoke("Executing\nRequest");

    /* from the forum
    Communications.makeWebRequest("tynbendad.github.io/.../response-login",
    {}, { :method => Communications.HTTP_REQUEST_METHOD_GET,
    :headers => { // set headers
    "Content-Type" => Communications.REQUEST_CONTENT_TYPE_URL_ENCODED},
    :responseType => Communications.HTTP_RESPONSE_CONTENT_TYPE_TEXT_PLAIN
    }, method(:onReceiveTestLogin));
    */

    var options =
    { :method => Communications.HTTP_REQUEST_METHOD_GET,
    :headers => { "Content-Type" => Communications.REQUEST_CONTENT_TYPE_URL_ENCODED },
    :responseType => Communications.HTTP_RESPONSE_CONTENT_TYPE_TEXT_PLAIN // was TYPE_JSON
    };

    //var reqStr = "">jsonplaceholder.typicode.com/.../" + reqN;

    // GPX file test
    //var reqStr = "
    ">drive.google.com/.../view

    // one line test file test
    var reqStr = "">drive.google.com/.../view

    System.println("request string = " + reqStr );

    Communications.makeWebRequest( reqStr, {}, options, method(:onReceive) );


    }

  • -400 is "Response body data is invalid for the request type."

    HTTP_RESPONSE_CONTENT_TYPE_TEXT_PLAIN is only available on API 3.0.0 and above - what device are you testing on?

    what is the mime type of the packet? Needs to be text/plain. 

    Your request looks correctly structured, although you might try null instead of {} in the second parameter:

    Communications.makeWebRequest( reqStr,null, options, method(:onReceive) );

    [EDIT]

    I really would encourage you to consider using json.

  • your still using a View link for the google file not a download link this needs to be corrected try your code with the second URL Below

    https://drive.google.com/file/d/1O1mqK0s8rNURO-6pOUozcgLDuDAHuHql/view?usp=sharing
    This Line will take you to the Google Site
    
    
    This Line Will take you to the File
    https://drive.google.com/uc?export=view&id=1O1mqK0s8rNURO-6pOUozcgLDuDAHuHql
    

  • +1 JSON is a lot easier once you get the hang of it and is a standard format so no parsing required inside your app

  • This retrieves the MAG Var for a location in json. the lat/lon I store in "semicircles" which I called Billis before I found the Garmin notation.

    degsToBillis = 0x7fffffff/180;
    the url is 

    var url= Ui.loadResource(Rez.Strings.loadMagVarsUrl)
      			+"?lat1="+engine.lat.toFloat()/degsToBillis 
      			+"&lon1="+engine.lon.toFloat()/degsToBillis 
      			+"&resultFormat=json";
    	    var headers = {
    			"Content-Type" => Comm.REQUEST_CONTENT_TYPE_URL_ENCODED			
    		};
    		Comm.makeWebRequest(
    			url,
    	 		null,
    			{   :headers => headers,
    				:method => Comm.HTTP_REQUEST_METHOD_GET,
    				:responseType => Comm.HTTP_RESPONSE_CONTENT_TYPE_JSON
    			},
    			method(:receiveMagVars)
    		) ;

  • Thanks very much Robin.

    Using the link you provided does make it work.

    Google drive does not produce a link in that form.

    Can you tell me what I need to know to produce a "download link" vs a "view link" or point me in the right direction ?

    Much apprecated.

    Jeff

  • No problem, you just need to change the front part of the URL then add the File ID, this article explains it https://www.google.com/amp/s/www.howtogeek.com/747810/how-to-make-a-direct-download-link-for-google-drive-files/amp/

  • Great. Exactly what I needed.

    Do you know if there's a similar issue with OneDrive, DropBox, etc ??

    Thanks again.

  • Thanks all.

    Much appreciated

  • Yes they pretty much all by default give you a link that takes you to a landing page for the file and not the file itself. You can code around this so users can just enter the link from the service like you had initially, or as suggested by some of the other users if you feel up to it create a website that will handle the conversion of GPX files to JSON