Quantcast
Channel: Active questions tagged rest - Stack Overflow
Viewing all articles
Browse latest Browse all 3753

How do I return a value from an async function in c# [duplicate]

$
0
0

I'm getting REST data from a website using c# and I'd like to return a value from a function call. But I need to use httpClient.SendAsync, so the function has to be public async. But I'm getting an error "The return type of an async method must be void. Is there anyway of getting around this?

    public async string GetTicketValue(string field, string ticketId)    {        string restUrl = String.Format("https://jira/rest/api/2/issue/{0}?", ticketId);        string classfield = String.Empty;        using (var httpClient = new HttpClient())        {            using (var request = new HttpRequestMessage(HttpMethod.Get, restUrl))            {                string base64Credentials = GetEncodedCredentials();                // Works to update ticket...                request.Headers.TryAddWithoutValidation("Authorization", $"Basic {base64Credentials}");                var response = await httpClient.SendAsync(request);                var str = await response.Content.ReadAsStringAsync();                JObject obj = (JObject)JsonConvert.DeserializeObject(str);                classfield = (string)obj["fields"][field]["value"];                Console.WriteLine("Field Value: {0}", classfield);            }        }        return classfield;    }

Viewing all articles
Browse latest Browse all 3753

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>