Home > OS >  Trying to make a request to an API with System.Net.Http but getting errors
Trying to make a request to an API with System.Net.Http but getting errors

Time:08-12

Here is the code I'd like to run:

    public async String products()
    {
        string res = await client.GetStringAsync(apiUrl "/allItems.php");
        
        return res;
    }

But I get an error like this:

The return type of an async method must be void, Task, Task, a task-like type, IAsyncEnumerable, or IAsyncEnumerator
StripeTerminal
C:\Users\Foxx\Documents\StripeTerminal\StripeTerminal\Form1.cs

What do I need to do to fix this?

CodePudding user response:

The error you posted says what the issue is. Take a look at your method signature. It's an async method, returning a string. Should be an async method returning a Task of type string.

Your res variable is of type string, that is correct. But an async method should always return a Task, where T is the underlying datatype you wish to return.

Full example is this.

public async Task<string> products()
{
    string res = await client.GetStringAsync(apiUrl "/allItems.php");
            
    return res;
}
  • Related