0

我正在使用 JotForms 使用 WebHook 集成发送 POST 消息。这是发送的消息。

RequestBin 消息

我正在使用 Azure Functions 在 C# 中实现 WebService,以便将表单中的值插入 SQL-Server。

    #r "System.Data"
    using System.Net;
using System.Data;
using System.Data.SqlClient;
using Newtonsoft.Json;

public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, TraceWriter log)
{
    log.Info("C# HTTP trigger function processed a request.");

    string name = "";

    dynamic body = await req.Content.ReadAsStringAsync();
    log.Info(body);

    var e = JsonConvert.DeserializeObject<Person>(body as string);

    name = e.firstname + " " + e.lastname;

    //Connect to SQL

    var cnnString  = "Server=tcp:XXXX.database.windows.net,1433;"+"Initial Catalog=XXXX;"
+"Persist Security Info=False;"+"User ID=XXX;"+"Password=XXXX;"+"MultipleActiveResultSets=False;"
+"Encrypt=True;"+"TrustServerCertificate=False;"+"Connection Timeout=30;";

    using (SqlConnection conn = new SqlConnection(cnnString))
    {
        conn.Open();

        // Insert Signup        
        var signupInsert = "INSERT INTO [dbo].[test_data] ([firstname],[lastname],[date])" +  
        "VALUES ('" + e.q8_FirstName + "','" + e.q9_yTus + "','" + e.q24_Birthday + "')";

        // Execute and load data into database.
        using (SqlCommand cmd = new SqlCommand(signupInsert, conn))
        {
            var rows = cmd.ExecuteNonQuery();
        }

    return name == " "
        ? req.CreateResponse(HttpStatusCode.BadRequest, "Please pass a name on the query string or in the request body")
        : req.CreateResponse(HttpStatusCode.OK, "Ok" );
    }
}

public class Person{
    public string firstname {get;set;}
    public string lastname {get;set;}
    public string date {get;set;}

    public string q8_FirstName {get;set;}
    public string q9_yTus {get;set;}
    public string q24_Birthday {get;set;}

}

当我使用只是 JSON 的主体进行测试时,我已成功插入

{
    "firstname": "ni\u00f1o",
    "lastname": "Lachner2",
    "date":"08/08/1994",

    "name":"Vincenz2",
    "slug":"submit\/80565829893878\/",
    "input_language":"Espa\u00f1ol",
    "q8_FirstName":"Nombre",
    "q9_yTus":"Apellido",
    "q24_Birthday":"08\/08\/1994",
    "q5_cedula":"115850853",
    "q18_country":"Costa Rica",
    "dropdown_search":"",
    "q3_province":"San Jos\u00e9",
    "q11_cantonSanJose":"Alajuelita",
    "q12_cantonAlajuela":"",
    "q13_cantonHeredia":"",
    "q14_cantonCartago":"",
    "q15_cantonPuntarenas":"",
    "q16_cantonPuntarenas":"",
    "q17_cantonGuanacaste":"",
    "q6_phone":"88141833",
    "q2_mail":"vincenz.lachner@gmail.com",
    "q7_shirtSize":"S",
    "q25_channel":{"0":"Correo electr\u00f3nico","other":"YOU"},
    "q27_politicaDe":"Accepted",
    "preview":"true"

}

如何访问 rawRequest 中的 JSON?那是在名为 rawRequest 的 RawBody 中。

这些分隔符是什么?--------------------------e5d83c25c3d6dcc0

--------------------------e5d83c25c3d6dcc0
Content-Disposition: form-data; name="rawRequest"

{"slug":"submit\/80565829893878\/","input_language":"Espa\u00f1ol","q8_FirstName":"textbox_sample0","q9_yTus":"textbox_sample1","q24_Birthday":"11\/11\/1111","q5_cedula":"1","q18_country":"Costa Rica","dropdown_search":"","q3_province":"San Jos\u00e9","q11_cantonSanJose":"Alajuelita","q12_cantonAlajuela":"","q13_cantonHeredia":"","q14_cantonCartago":"","q15_cantonPuntarenas":"","q16_cantonPuntarenas":"","q17_cantonGuanacaste":"","q6_phone":"1","q2_mail":"john@example.com","q7_shirtSize":"XS","q25_channel":["Facebook"],"q27_politicaDe":"Accepted","preview":"true"}
4

2 回答 2

1

如果您从 JotForms 发送消息,您可以使用以下代码获取 rawRequest,或者您可以获得漂亮的属性值,即 JotForms 中的字段。

以下是我的详细步骤:

1.创建JotForms

在此处输入图像描述

2.添加如下代码获取rawRequest

  if (req.Content.IsMimeMultipartContent())
     {
          var content = await req.Content.ReadAsMultipartAsync();
          var test = content.Contents.ToList();
          Dictionary<string, string> dic = new Dictionary<string, string>();
          foreach (var item in test)
          {
              var value = await item.ReadAsStringAsync();
              dic.Add(item.Headers.ContentDisposition.Name, value);
              log.Info(value);
          }
          foreach (var item in dic)
          {
             log.Info($"{item.Key}:{item.Value}");
          }
     }

3.我这边的远程调试。

在此处输入图像描述

于 2018-02-27T10:00:03.277 回答
1

它应该是这样的

var form = await req.Content.ReadAsFormDataAsync();
var body = form["rawRequest"];
于 2018-02-27T08:43:31.460 回答