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?

  • NETWORK_RESPONSE_TOO_LARGE -402

    API Level 1.0.0

    Serialized response was too large.

    The amount of data you're requesting is too large.

    If you can't limit the data returned by way of parameters in the request, you'll need a proxy to limit the data

  • But actually,the response json is by no means large ,it is a quite small with 1 key

  • How small?  <8k?  Are you sure you're not getting something like an error screen?

  • In my callback function,I tried to print the data I received from remote server.it’s null.

  • Maybe the network connection is poor towards the target server. I will try to use a vpn. lol 

  • That's normal when you get an error.  The data you got back can't be processed.  In the sim, have you tried File>View HTTP Traffic?

    How much available memory does your app have?

  • I haven't tried to build their app, but the response for the URL in the app is literally this:

    {"dogecoin":{"cny":1.95,"cny_24h_change":11.039604337661984}}

    You can reproduce with this:

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

    I did notice this snippet of code:

       var options = {
                :method => Communications.HTTP_REQUEST_METHOD_GET,
                :responseType => Communications.HTTP_RESPONSE_CONTENT_TYPE_JSON,
                :headers => {
                    "accept" => Communications.HTTP_RESPONSE_CONTENT_TYPE_JSON
                }
            };
    
    I don't think '"accept" => Communications.HTTP_RESPONSE_CONTENT_TYPE_JSON' is correct. "Communications.HTTP_RESPONSE_CONTENT_TYPE_JSON" is literally 0. It's a Monkey C SDK enum, it's not literally "application/json". However, the following command also works with no problem:

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

    But you could try removing the ":headers" option from the code.

  • I don't think '"accept" => Communications.HTTP_RESPONSE_CONTENT_TYPE_JSON' is correct. "Communications.HTTP_RESPONSE_CONTENT_TYPE_JSON" is literally 0. It's a Monkey C SDK enum, it's not literally "application/json".

    That is fine. We translate the options values into strings in the virtual machine.

  • I didn't realize that putting in a literal header of "accept" with "Communications.HTTP_RESPONSE_CONTENT_TYPE_JSON" would work. Thanks!

    I thought setting the responseType would cause the accept header to be set automatically. (But I haven't written any apps with network requests, so it was just conjecture.)

  • Whoops. That won't work. I thought this was "Content-Type", not "accept". You are right, that won't work.

    To do this right, you want to specify the :responseType attribute in the options dict (which is already there and is translated) and then you need to write out "application/json" for the "accept" header value.