'Can't exit function properly

I have a C# unity function and I'm trying to request some json from a webserver but for some reason its not exiting the function where I want it to:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using Proyecto26;
using UnityEditor;
using UnityEngine.Networking;
using System.Text;
using TMPro;
using SimpleJSON;

public class APIRequest
{
   // string basicRequestPage = "****";
    string basicAPI_Key = "****";
    // Start is called before the first frame update

    string outputs;


    public string basicRequest(int requestTyp)
    {


        auth test1 = new auth();

        test1.key = basicAPI_Key;


        switch (requestTyp)
        {
            case 0:
                test1.request = "deckList";
                break;
            case 1:
                test1.request = "card";
                break;

        }
        string output = JsonConvert.SerializeObject(test1);

        byte[] outputToServer = Encoding.ASCII.GetBytes(output); ;


        //Debug.Log(output);

        RestClient.Request(new RequestHelper
        {
            Uri = basicRequestPage,
            Method = "POST",
            Timeout = 10,
            //EnableDebug = true,
            ParseResponseBody = false, //Don't encode and parse downloaded data as JSONe
            BodyRaw = outputToServer
        }).Then(response =>
        {
            // EditorUtility.DisplayDialog("Status", response.Text, "Ok");
            var rest = response.Text;
            //Debug.Log(rest);
            outputs = rest.ToString();
            //EditorUtility.DisplayDialog("Status", outputs, "Ok");
            return outputs;

        }).Catch(err =>
        {
            var error = err as RequestException;
            EditorUtility.DisplayDialog("Error Response", error.Response + "", "Ok");


        }


        );


    }
}

Link to code because it wouldn't work with the built in code text

It works perfectly fine as a method but keeps complaining that I don't have a valid return from the function. When I try to return the value I get from API, namely the outputs variable, it says it null. I have tried putting it in the function, in a public variable. but to now avail.

Any ideas?



Solution 1:[1]

When you're working with promises you need to return a promise from that method too (Promise chaining):

public IPromise<string> basicRequest(int requestTyp)
{
    return RestClient.Request(new RequestHelper
    {
        Uri = basicRequestPage,
        Method = "POST",
        Timeout = 10,
        //EnableDebug = true,
        ParseResponseBody = false, //Don't encode and parse downloaded data as JSONe
        BodyRaw = outputToServer
    }).Then(response => response.Text.ToString());
}

For more details, check the repository of the Promises library used by the RestClient for Unity here.

Solution 2:[2]

You have defined an output of type string however you do not return a string at all exits. After the catch you have to return a string either at the end of the method or in the catch. The last line I have added return ""; should solve the problem.

public string basicRequest(int requestTyp)
{
    auth test1 = new auth();
    test1.key = basicAPI_Key;
    switch (requestTyp)
    {
        case 0:
            test1.request = "deckList";
            break;
        case 1:
            test1.request = "card";
            break;

    }
    string output = JsonConvert.SerializeObject(test1);
    byte[] outputToServer = Encoding.ASCII.GetBytes(output);

    //Debug.Log(output);

    RestClient.Request(new RequestHelper
    {
        Uri = basicRequestPage,
        Method = "POST",
        Timeout = 10,
        //EnableDebug = true,
        ParseResponseBody = false, //Don't encode and parse downloaded data as JSONe
        BodyRaw = outputToServer
    }).Then(response =>
    {
        // EditorUtility.DisplayDialog("Status", response.Text, "Ok");
        var rest = response.Text;
        //Debug.Log(rest);
        outputs = rest.ToString();
        //EditorUtility.DisplayDialog("Status", outputs, "Ok");
        return outputs;

    }).Catch(err =>
    {
        var error = err as RequestException;
        EditorUtility.DisplayDialog("Error Response", error.Response + "", "Ok");
    }
    );
    return ""; //<----- this here
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 jdnichollsc
Solution 2 dennis_ler