Is it possible to create HTTP(s) post request inside Azure Function? I am trying to create a custom webhook that is listening to one service and when triggered then its calling another service over HTTP using post.
My code looks like that:
public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, TraceWriter log)
{
BitbucketRequest data = await req.Content.ReadAsAsync<BitbucketRequest>();
//DO STH WITH DATA TO GET e.g. USER STORY ID
using(var client = new HttpClient()){
client.BaseAddress = new Uri("https://SOME_TARGETPROCESS_URL/api/v1");
var body = new { EntityState = new { Id = 174 } };
var result = await client.PostAsJsonAsync(
"/UserStories/7034/?resultFormat=json&access_token=MYACCESSTOKEN",
body);
string resultContent = await result.Content.ReadAsStringAsync();
}
return req.CreateResponse<string>(HttpStatusCode.OK,"OKOK");
}
I suppose the problem is that currently HttpRequestMessage is occupying web socket and I can not create new Http Request.
Errors that I found in Exceptions details:
- The underlying connection was closed: An unexpected error occurred on a send.
- Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host.
- Socket Exception Error Code : 10054
I just spent quite a few hours myself trying to get this to work. This was in NodeJS. The thing I figured out, was that I apparently needed to have an endpoint running HTTPS, and with a valid certificate.
Not sure if this is documented anywhere.
It is certainly possible and the following code block works just fine in my test function:
It prints out HTML of google.com.
POST
also works: returns Error 405 (Method Not Allowed)!!1 from google.Can it be that your callee is failing?
I've done the HTTP post inside Azure Function like so:
As you could guess by the name, I'm using this to send a POST request to a telegram bot