Failed to decode call arguments in query JS plain call

What type of topic is this?
Bug Report

This is my worked code(await ICPagent.fetchRootKey() solve previous problem:

  import { AuthClient } from "https://cdn.jsdelivr.net/npm/@dfinity/auth-client@1.3.0/+esm";
  import { HttpAgent } from "https://cdn.jsdelivr.net/npm/@dfinity/agent@1.3.0/+esm";

  const ICPauthClient = await AuthClient.create();
  window.ICPauthClient = ICPauthClient
  import isomorphicFetch from 'https://cdn.jsdelivr.net/npm/isomorphic-fetch@3.0.0/+esm'
  const host = 'local' === 'local' ? 'http://127.0.0.1:4943' : 'https://icp-api.io';


  const ICPagent = new HttpAgent({ isomorphicFetch, host }); //isomorphicFetch, host, fetchOptions });
  await ICPagent.fetchRootKey()
  console.log("ICPagent " + JSON.stringify(ICPagent))
  window.ICPagent = ICPagent

  const get_counter = await window.ICPagent.query(
    'be2us-64aaa-aaaaa-qaabq-cai',
    {
      methodName: 'get_counter',
      arg: new ArrayBuffer(0),
    },
  )
  console.log("get_counter " + JSON.stringify(get_counter))

But it produse new error:

get_counter {β€œstatus”:β€œrejected”,β€œerror_code”:β€œIC0503”,β€œreject_code”:5,β€œreject_message”:β€œIC0503: Canister be2us-64aaa-aaaaa-qaabq-cai trapped explicitly: failed to decode call arguments: Custom(Cannot parse header \n\nCaused by:\n binary parser error: io error)”,β€œsignatures”:[{β€œtimestamp”:β€œ1715333024548043000”,β€œsignature”:{β€œ0”:154,β€œ1”:119,β€œ2”:93,β€œ3”:195,β€œ4”:99,β€œ5”:202,β€œ6”:18,β€œ7”:48,β€œ8”:182,β€œ9”:34,β€œ10”:170,β€œ11”:34,β€œ12”:104,β€œ13”:172,β€œ14”:171,β€œ15”:1,β€œ16”:197,β€œ17”:21,β€œ18”:118,β€œ19”:127,β€œ20”:163,β€œ21”:148,β€œ22”:241,β€œ23”:87,β€œ24”:115,β€œ25”:149,β€œ26”:252,β€œ27”:156,β€œ28”:217,β€œ29”:171,β€œ30”:129,β€œ31”:137,β€œ32”:192,β€œ33”:226,β€œ34”:104,β€œ35”:170,β€œ36”:76,β€œ37”:144,β€œ38”:68,β€œ39”:205,β€œ40”:172,β€œ41”:146,β€œ42”:124,β€œ43”:116,β€œ44”:135,β€œ45”:10,β€œ46”:23,β€œ47”:144,β€œ48”:190,β€œ49”:150,β€œ50”:6,β€œ51”:105,β€œ52”:121,β€œ53”:77,β€œ54”:72,β€œ55”:255,β€œ56”:95,β€œ57”:43,β€œ58”:137,β€œ59”:186,β€œ60”:127,β€œ61”:68,β€œ62”:10,β€œ63”:10},β€œidentity”:{β€œ0”:237,β€œ1”:78,β€œ2”:21,β€œ3”:221,β€œ4”:245,β€œ5”:204,β€œ6”:213,β€œ7”:51,β€œ8”:231,β€œ9”:76,β€œ10”:3,β€œ11”:98,β€œ12”:24,β€œ13”:55,β€œ14”:119,β€œ15”:87,β€œ16”:127,β€œ17”:245,β€œ18”:29,β€œ19”:228,β€œ20”:215,β€œ21”:14,β€œ22”:33,β€œ23”:7,β€œ24”:157,β€œ25”:118,β€œ26”:74,β€œ27”:7,β€œ28”:2}}],β€œhttpDetails”:{β€œok”:true,β€œstatus”:200,β€œstatusText”:β€œOK”,β€œheaders”:[[β€œcontent-length”,β€œ389”],[β€œcontent-type”,β€œapplication/cbor”]]},β€œrequestId”:{}}

I try to do args like and it also doesn’t work.

Usually you create an Actor using the HttpAgent instead of making calls directly. Why are you not doing this here?

The arguments to HttpAgent.query can be found here. The docs for arg state

A binary encoded argument. This is already encoded and will be sent as is.

So I assume you would need to candid encode your arg before passing it to query. Maybe @kpeacock can add more context

definitely seconding @cryptoschindler here - crafting your own query is an advanced use case. Even I will avoid it as much as I possibly can. It’s much easier to use an actor with IDL.

As a side note, you should avoid calling fetchRootKey without conditional logic. It should only be called if you are on a local network, since the mainnet root key is hard coded into the agent, and if it ever fails on a mainnet call, it could imply that the network has been compromised, potentially through a malicious boundary node.

I highly recommend getting started with the intro to javascript guide!

1 Like