how to access web server on localhost

I try to connect from simulator to web server running on the same computer where simulator. Firefox/Chrome can access page http://localhost but when I try

makeWebRequest on simulator with http://localhost address I get 404 error.

Should I change any setting in simulator?

  • Do you think I didn't try? Of course I did, and not run!

    Do you think I didn't read this thread? Of course I did, but is very long without conclusion

    SDK 4.0.6.

    HTTP Traffic logs - empty.

    I tried all combination:

    - makeWebRequest(http/https...

    - on/off option in simulator [Settings][Use Device HTTPS...].

    And to clarify, its built php server running on windows. I checked 80, 443 and 800 ports. Always 404.

  • I was curious about this so, I tried this on my own computer:

    - Windows 11

    - VS Code 1.62.3

    - Monkey C plugin 1.0.0

    - CIQ SDK 4.0.6

    - node 14.16.1

    App class:

    import Toybox.Application;
    import Toybox.Lang;
    import Toybox.WatchUi;
    
    class webReqApp extends Application.AppBase {
    
        function initialize() {
            AppBase.initialize();
        }
    
        // onStart() is called on application start up
        function onStart(state as Dictionary?) as Void {
        }
    
        // onStop() is called when your application is exiting
        function onStop(state as Dictionary?) as Void {
        }
    
        // Return the initial view of your application here
        function getInitialView() as Array<Views or InputDelegates>? {
            return [ new webReqView() ] as Array<Views or InputDelegates>;
        }
    
    }
    
    function getApp() as webReqApp {
        return Application.getApp() as webReqApp;
    }

    View class:

    import Toybox.Graphics;
    import Toybox.WatchUi;
    import Toybox.Communications;
    
    class webReqView extends WatchUi.View {
    
        function initialize() {
            View.initialize();
            Communications.makeWebRequest("http://localhost", {}, {}, method(:onReceive));
        }
    
       function onReceive(responseCode, data) {
           if (responseCode == 200) {
               System.println("Request Successful: " + data);
           }
           else {
               System.println("Response: " + responseCode);
           }
        }
        // Load your resources here
        function onLayout(dc as Dc) as Void {
            setLayout(Rez.Layouts.MainLayout(dc));
        }
    
        // Called when this View is brought to the foreground. Restore
        // the state of this View and prepare it to be shown. This includes
        // loading resources into memory.
        function onShow() as Void {
        }
    
        // Update the view
        function onUpdate(dc as Dc) as Void {
            // Call the parent onUpdate function to redraw the layout
            View.onUpdate(dc);
        }
    
        // Called when this View is removed from the screen. Save the
        // state of this View here. This includes freeing resources from
        // memory.
        function onHide() as Void {
        }
    
    }

    Web server:

    - I created a folder called srv/ and placed the following file in it:

    index.json:

    { "foo": "bar2" }
    

    - I installed http-server for node:

    npm install -g http-server

    - I changed to srv/ and ran http-server

    http-server -p 80 -e json

    - Verified that http://localhost works in both chrome and firefox

    - Also tried curl:

    curl localhost

    { "foo": "bar2" }

    - Built and ran the monkey c app described above. Of course the the request failed with a -1001 error (due to lack of https)

    - Opened the simulator's Settings menu and unchecked Use Device HTTPS requirements

    - Ran the app again and the output was:

    Request Successful: {foo=>bar2}

    Sorry, I know this answer must be pretty useless to you. Next I won't bother trying to help.

  • I salute your approach. Thanks for taking the time to put this together. It's a lot useful to me.

  • One hint...

    My problem was with web server! When I start it with parameter:

    address=LOCALHOST

    it is not accessible on 127.0.0.1 it should be:

    address=127.0.0.1