如何通过POST将参数传递给Azure函数? [英] How to pass parameters by POST to an Azure function?
问题描述
我正在尝试做一个简单的Azure函数来了解它.将有3个功能:
I'm trying to do a simple Azure Function to learn about it. There will be 3 functions:
- 1函数,用于将行插入数据库的表中.该表将包含当前日期和由用户键入并由GET传递的字符串参数.
- 1个功能与上一个功能相似,但通过POST传递参数.
- 1个功能来读取表格并显示其内容.
我已经可以做第一个和第三个了.但是我无法通过POST传递参数.我一直在寻找示例,但是我无法成功地运行它们.客户端应用程序是Windows窗体应用程序.
I've been able to do the first and the third ones. But I can't pass the parameter by POST. I've looked for examples but I couldn't run them with success. The client app is a Windows Forms one.
谁能给我一个示例,说明如何通过POST将参数传递给函数以及如何读取它们?
Could anyone show me an example anout how to pass parameters by POST to the function and how to read them?
先感谢
以下是通过GET传递参数的代码(工作正常):
Here's the code to pass the parameters by GET (this is working fine):
private void button2_Click(object sender, EventArgs e)
{
string cadena = lsql1.Text + "?notas=" + tNotas.Text;
try
{
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(cadena);
HttpWebResponse res = (HttpWebResponse)req.GetResponse();
if (res.StatusCode == HttpStatusCode.OK)
{
MessageBox.Show("Grabado");
}
else
{
MessageBox.Show(res.StatusDescription);
}
}catch (WebException ex)
{
using (Stream s = ex.Response.GetResponseStream())
{
StreamReader sr = new StreamReader(s);
string text = sr.ReadToEnd();
text = text.Substring(1, text.Length - 2);
sr.Close();
text = text.Replace("\\", "");
text = "{" + text + "}";
Error mensajeError = JsonConvert.DeserializeObject<Error>(text);
MessageBox.Show(mensajeError.ExceptionMessage);
}
}
}
这是接收它并进行插入的代码(这也起作用):
And here's the code to receive it and do the insert (this is working too):
[FunctionName("sql1")]
public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, TraceWriter log)
{
try
{
log.Info("C# HTTP trigger function processed a request.");
var cnnString = "Server=SERVIDOR;Database=base_prueba;User ID =azure;Password=0000;Trusted_Connection=False;Encrypt=False;";
using (SqlConnection connection = new SqlConnection(cnnString))
{
connection.Open();
SqlCommand cmd = connection.CreateCommand();
DateTime fecha = DateTime.Today;
string notas = req.GetQueryNameValuePairs()
.FirstOrDefault(q => string.Compare(q.Key, "notas", true) == 0)
.Value;
// insert a log to the database
cmd.CommandText = "INSERT INTO Prueba_Azure (fecha, notas) VALUES ('" + fecha.ToString() + "', '" + notas + "')";
cmd.ExecuteNonQuery();
}
// Get request body
dynamic data = await req.Content.ReadAsAsync<object>();
return name == req.CreateResponse(HttpStatusCode.OK, "Done");
}
catch (Exception ex)
{
HttpResponseMessage res = req.CreateErrorResponse(HttpStatusCode.InternalServerError, ex);
return res;
}
}
我正在寻找的是通过POST
What I'm looking for is to to this by POST
推荐答案
要从请求正文(后请求)获取请求内容,可以使用req.Content.ReadAsAsync方法.这是代码示例.
To get the request content from the request body(post request), you could use req.Content.ReadAsAsync method. Here is the code sample.
示例请求正文.
{
"name": "Azure"
}
定义一个类以反序列化发布数据.
Define a class to deserialize the post data.
public class PostData
{
public string name { get;set; }
}
获取帖子数据并显示.
PostData data = await req.Content.ReadAsAsync<PostData>();
log.Info("name:" + data.name);
发送发帖请求的客户端代码.
Client side code to send the post request.
HttpWebRequest req = (HttpWebRequest)WebRequest.Create("function-url");
req.Method = "POST";
req.ContentType = "application/json";
Stream stream = req.GetRequestStream();
string json = "{\"name\": \"Azure\" }";
byte[] buffer = Encoding.UTF8.GetBytes(json);
stream.Write(buffer,0, buffer.Length);
HttpWebResponse res = (HttpWebResponse)req.GetResponse();
这篇关于如何通过POST将参数传递给Azure函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!