Web request always return -402

Hi, I encountered a problem when executing a web request.

The api is

curl -X GET "https://api.coingecko.com/api/v3/simple/price?ids=dogecoin&vs_currencies=cny" -H "accept: application/json"

code: https://github.com/Likenttt/dogecoin-is-flying-to-the-moon-with-elon-mask

My code will always get -402. why?

I cannot paste my code in text for the unknown sake, please check the repo above.

What's this?

  • This works just fine, both for pasting and for testing.

    import Toybox.Application;
    import Toybox.Communications;
    import Toybox.Graphics;
    import Toybox.Time;
    import Toybox.WatchUi;
    import Toybox.Lang;
    
    class DogecoinDelegate extends WatchUi.BehaviorDelegate {
    
        hidden var _view as DogecoinView;
    
        function initialize(view as DogecoinView) {
            BehaviorDelegate.initialize();
            _view = view;
        }
        
        function onSelect() as Boolean {
            _view.fetchPrice();
            return true;
        }
    }
    
    class DogecoinView extends WatchUi.View {
    
        hidden var currencyType as String = "cny";
    
        function initialize() {
            View.initialize();
        }
    
        function fetchPrice() as Void {
            var url = "https://api.coingecko.com/api/v3/simple/price";
    
            var options = {
                :method => Communications.HTTP_REQUEST_METHOD_GET,
                :responseType => Communications.HTTP_RESPONSE_CONTENT_TYPE_JSON,
                :headers => {
                },
            };
           
            var params = {
                "ids" => "dogecoin",
                "vs_currencies" => currencyType,
                "include_24hr_change" => "true",
            };
    
            Communications.makeWebRequest(
                url,
                params,
                options,
                method(:onReceive)
            );
        }
    
        function onReceive(responseCode as Number, data as Dictionary?) as Void {
            System.println(responseCode);
            System.println(data);
        }
    }
    
    class DogecoinApp extends Application.AppBase
    {
        function initialize() {
            AppBase.initialize();
        }
    
        function getInitialView() as Array<Views or InputDelegates>? {
            var view = new DogecoinView();
            return [view, new DogecoinDelegate(view)] as Array<Views or InputDelegates>;
        }
    
    }
    

    It displays nothing in the simulated device, but dumps this to the debugger console when clicking the start/stop button...

    200
    {dogecoin=>{cny=>1.970000, cny_24h_change=>11.401946}}

  • hidden var options = {
                :method => Communications.HTTP_REQUEST_METHOD_GET,
                :responseType => Communications.HTTP_RESPONSE_CONTENT_TYPE_JSON,
                :headers => {
                    "accept" => "application/json"
                }
            };
    Thanks, Bro. I make it. 

  • The usage “as String” looks like typescript. I have never seen before.Is it a new feature ?

  • That's monkey types.  New in the 4.0 SDK.

  • Oh,good job. Iuse Java most time in my daily work,in which case a static type gives me the sense of security.

  • Hey,Bro. I have another question not related to the technology. What motivates you to make a free program and keep improving it?

  • I get "buy me a beer" donations fairly often, and I'm retired, so it's a hobby for me.  I was a professional programmer for decades so it keeps me busy.

  • I have a day job so I don't have a lot of time to spend on CIQ. However, I did create a few apps that filled certain niches that were empty (at the time.)

    For me the motivation is just to make apps that ppl like and which don't already exist (in the exact same form.) It's nice seeing five star reviews, tbh, although the best I've done is 25k downloads or about 50 reviews. Nothing like a Dozen Run (400k downloads), a DWMap or a some of these million-download watchfaces.

    I do use a couple of my own apps, too. like Lap+ ("single run field" with lap list) and Stop and Go (a "better" stopwatch for your Garmin).

    If I had time, my next project would probably be a watchface, to see if I could actually design something that looks nice and gets a lot of downloads.

  • Iuse Java most time in my daily work,in which case a static type gives me the sense of security.

    Yeah, I hate runtime type errors.