Reputation: 514
I am having an Http triggered azure function which I am able to debug successfully using Postman by calling the api in postman and getting the desired response. Now I am trying to debug by calling the api from a separate method and that I am doing by calling the api in the main function something like this: Azure Function:
public static class Function1
{
[FunctionName("Function1")]
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
ILogger log)
{
log.LogInformation("C# HTTP trigger function processed a request.");
var requestBody = await new StreamReader(req.Body).ReadToEndAsync();
return new OkObjectResult("Test");
}
}
class Program
{
static async Task Main(string[] args)
{
HttpClient client = new HttpClient();
var requestUri = new Uri("http://localhost:7071/api/Function1");
var rec = "Test:test";
var response = await client.PostAsJsonAsync(requestUri, rec);
}}
When I run I get an error in the last line saying "target machine actively refused the connection". The url is same which I am passing in postman and when I do from postman, it hits my function method. What wrong am I doing here??
Note: I have not yet published my function as I am debugging locally now
Upvotes: 1
Views: 1260
Reputation: 14080
When I run I get an error in the last line saying "target machine actively refused the connection". The url is same which I am passing in postman and when I do from postman, it hits my function method. What wrong am I doing here??
First of all, please make sure your function runtime is still running.
And below code works fine on my side:
using System;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
HttpClient client = new HttpClient();
var requestUri = new Uri("http://localhost:7071/api/Function1");
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(requestUri);
request.Method = "POST";
string contents = "{\"Test\":\"test\"}";
byte[] content = Encoding.UTF8.GetBytes(contents);
int contentlength = content.Length;
using (Stream requestStream = request.GetRequestStream())
{
requestStream.Write(content, 0, (int)contentlength);
}
using (HttpWebResponse resp = (HttpWebResponse)request.GetResponse())
{
StreamReader reader = new StreamReader(resp.GetResponseStream());
string text = reader.ReadToEnd();
Console.WriteLine(text);
}
}
}
}
Upvotes: 1