Unity error Error: HTTP/1.1 400 Bad Request when performing SearchNFTs function

Hello everyone!

I am currently trying to learn Moralis and I have a problem: when performing the SearchNFTs function I get a HTTP Bad request error. While I can successfully retrieve user’s currency and the NFTs they bought given a specific contract, I cannot search for NFTs.

The code I use to perform the search is the one found here and is the following

using System;
using System.Collections.Generic;
using System.Linq;
using MoralisUnity;
using MoralisUnity.Platform.Objects;
using MoralisUnity.Web3Api.Models;
using UnityEngine;
using UnityEngine.InputSystem;

public class NftBrowserController : MonoBehaviour
{
    public async void SearchNft(TMPro.TMP_InputField inputField)
    {
        Debug.Log("Searching for " + inputField.text);
        try
        {
            NftMetadataCollection nfts = await Moralis.Web3Api.Token.SearchNFTs(inputField.text, ChainList.eth, filter: "global");
            Debug.Log(nfts.ToJson());
        }
        catch (Exception e)
        {

            Debug.Log(e);
        }
        
    }
}

The error I get is the following.

MoralisUnity.Web3Api.Client.ApiException: Error calling SearchNFTs: {"code":141,"error":"Invalid number string:  for variable"}
  at MoralisUnity.Web3Api.CloudApi.TokenApi.SearchNFTs (System.String q, MoralisUnity.Web3Api.Models.ChainList chain, System.String format, System.String filter, System.Nullable`1[T] fromBlock, System.Nullable`1[T] toBlock, System.String fromDate, System.String toDate, System.String cursor, System.Nullable`1[T] limit) [0x0027e] in D:\Unity\Projects\MoralisProject\MoralisProject\Packages\io.moralis.web3-unity-sdk\Runtime\Web3Api\CloudApi\TokenApi.cs:499 
  at Cysharp.Threading.Tasks.CompilerServices.AsyncUniTask`2[TStateMachine,T].GetResult (System.Int16 token) [0x00000] in D:\Unity\Projects\MoralisProject\MoralisProject\Packages\io.moralis.web3-unity-sdk\Runtime\External\UniTask\Runtime\CompilerServices\StateMachineRunner.cs:342 
  at NftBrowserController.SearchNft (TMPro.TMP_InputField inputField) [0x00074] in D:\Unity\Projects\MoralisProject\MoralisProject\Assets\_Scripts\NftRelated\NftBrowserController.cs:88 
UnityEngine.Debug:Log (object)
NftBrowserController/<SearchNft>d__6:MoveNext () (at Assets/_Scripts/NftRelated/NftBrowserController.cs:94)
System.Runtime.CompilerServices.AsyncMethodBuilderCore/MoveNextRunner:Run ()
Cysharp.Threading.Tasks.AwaiterActions:Continuation (object) (at Packages/io.moralis.web3-unity-sdk/Runtime/External/UniTask/Runtime/UniTask.cs:21)
Cysharp.Threading.Tasks.UniTaskCompletionSourceCore`1<MoralisUnity.Web3Api.Models.NftMetadataCollection>:TrySetException (System.Exception) (at Packages/io.moralis.web3-unity-sdk/Runtime/External/UniTask/Runtime/UniTaskCompletionSource.cs:167)
Cysharp.Threading.Tasks.CompilerServices.AsyncUniTask`2<MoralisUnity.Web3Api.CloudApi.TokenApi/<SearchNFTs>d__15, MoralisUnity.Web3Api.Models.NftMetadataCollection>:SetException (System.Exception) (at Packages/io.moralis.web3-unity-sdk/Runtime/External/UniTask/Runtime/CompilerServices/StateMachineRunner.cs:334)
MoralisUnity.Web3Api.CloudApi.TokenApi/<SearchNFTs>d__15:MoveNext () (at Packages/io.moralis.web3-unity-sdk/Runtime/Web3Api/CloudApi/TokenApi.cs:503)
Cysharp.Threading.Tasks.CompilerServices.AsyncUniTask`2<MoralisUnity.Web3Api.CloudApi.TokenApi/<SearchNFTs>d__15, MoralisUnity.Web3Api.Models.NftMetadataCollection>:Run () (at Packages/io.moralis.web3-unity-sdk/Runtime/External/UniTask/Runtime/CompilerServices/StateMachineRunner.cs:313)
Cysharp.Threading.Tasks.AwaiterActions:Continuation (object) (at Packages/io.moralis.web3-unity-sdk/Runtime/External/UniTask/Runtime/UniTask.cs:21)
Cysharp.Threading.Tasks.UniTaskCompletionSourceCore`1<System.Tuple`3<System.Net.HttpStatusCode, System.Collections.Generic.Dictionary`2<string, string>, string>>:TrySetResult (System.Tuple`3<System.Net.HttpStatusCode, System.Collections.Generic.Dictionary`2<string, string>, string>) (at Packages/io.moralis.web3-unity-sdk/Runtime/External/UniTask/Runtime/UniTaskCompletionSource.cs:139)
Cysharp.Threading.Tasks.CompilerServices.AsyncUniTask`2<MoralisUnity.Web3Api.Client.ApiClient/<CallApi>d__12, System.Tuple`3<System.Net.HttpStatusCode, System.Collections.Generic.Dictionary`2<string, string>, string>>:SetResult (System.Tuple`3<System.Net.HttpStatusCode, System.Collections.Generic.Dictionary`2<string, string>, string>) (at Packages/io.moralis.web3-unity-sdk/Runtime/External/UniTask/Runtime/CompilerServices/StateMachineRunner.cs:328)
MoralisUnity.Web3Api.Client.ApiClient/<CallApi>d__12:MoveNext () (at Packages/io.moralis.web3-unity-sdk/Runtime/Web3Api/Client/ApiClient.cs:137)
Cysharp.Threading.Tasks.CompilerServices.AsyncUniTask`2<MoralisUnity.Web3Api.Client.ApiClient/<CallApi>d__12, System.Tuple`3<System.Net.HttpStatusCode, System.Collections.Generic.Dictionary`2<string, string>, string>>:Run () (at Packages/io.moralis.web3-unity-sdk/Runtime/External/UniTask/Runtime/CompilerServices/StateMachineRunner.cs:313)
Cysharp.Threading.Tasks.AwaiterActions:Continuation (object) (at Packages/io.moralis.web3-unity-sdk/Runtime/External/UniTask/Runtime/UniTask.cs:21)
Cysharp.Threading.Tasks.UniTaskCompletionSourceCore`1<System.Tuple`3<System.Net.HttpStatusCode, System.Collections.Generic.Dictionary`2<string, string>, string>>:TrySetResult (System.Tuple`3<System.Net.HttpStatusCode, System.Collections.Generic.Dictionary`2<string, string>, string>) (at Packages/io.moralis.web3-unity-sdk/Runtime/External/UniTask/Runtime/UniTaskCompletionSource.cs:139)
Cysharp.Threading.Tasks.CompilerServices.AsyncUniTask`2<MoralisUnity.Web3Api.UniversalWebClient/<ExecuteAsync>d__7, System.Tuple`3<System.Net.HttpStatusCode, System.Collections.Generic.Dictionary`2<string, string>, string>>:SetResult (System.Tuple`3<System.Net.HttpStatusCode, System.Collections.Generic.Dictionary`2<string, string>, string>) (at Packages/io.moralis.web3-unity-sdk/Runtime/External/UniTask/Runtime/CompilerServices/StateMachineRunner.cs:328)
MoralisUnity.Web3Api.UniversalWebClient/<ExecuteAsync>d__7:MoveNext () (at Packages/io.moralis.web3-unity-sdk/Runtime/Web3Api/UniversalWebClient.cs:113)
Cysharp.Threading.Tasks.CompilerServices.AsyncUniTask`2<MoralisUnity.Web3Api.UniversalWebClient/<ExecuteAsync>d__7, System.Tuple`3<System.Net.HttpStatusCode, System.Collections.Generic.Dictionary`2<string, string>, string>>:Run () (at Packages/io.moralis.web3-unity-sdk/Runtime/External/UniTask/Runtime/CompilerServices/StateMachineRunner.cs:313)
Cysharp.Threading.Tasks.Internal.PooledDelegate`1<UnityEngine.AsyncOperation>:Run (UnityEngine.AsyncOperation) (at Packages/io.moralis.web3-unity-sdk/Runtime/External/UniTask/Runtime/Internal/PooledDelegate.cs:46)
UnityEngine.AsyncOperation:InvokeCompletionEvent ()

Does anyone have a possible solution for this? Thanks in advance!

Hello which version of the Unity SDK / game kit are you using? What is the value of inputField.text?

Going from the docs, the search query value needs a q key; the example given is:

NftMetadataCollection nft = await Moralis.Web3Api.Token.SearchNFTs(q: "Pancake", ChainList.bsc, filter: "name");

Thank you for your answer!

The Unity SDK I am using is the v1.2.5 which I found here

I tried to insert the q key but it is still not working. Even hardcoding the string (like “pancakes”) instead of taking it from the inputfield gives the same error.

Here is some more information:

  • The Unity version I am currently using is the 2021.3.4f1
  • I am running the application from the editor
  • Once created the dapp I selected “All chains” on the mainnet

If there is more information I can provide please ask away

Can you post your code that you’ve tried now? Can you try other parameters e.g. different chain and filter?