[英]How to pass a parameter using vba to an asp.net web api?
I'm trying to use vba to post to a web api.我正在尝试使用 vba 发布到 web api。 The problem is that the parameter is blank in the web api.问题是web api中的参数为空。 The web api is written in asp. web api是用asp编写的。 I called the api from c# and it works but I can't get it to work from vba.我从 c# 调用了 api,它可以工作,但我无法从 vba 中获得它。
Here is the vba call to the api:这是对 api 的 vba 调用:
Sub Test()
Dim http As New MSXML2.XMLHTTP60
http.Open "POST", "http://10.5.72.172:108/api/vbaemail", True
http.SetRequestHeader "Content-type", "application/json"
Call http.send("abc")
End Sub
Here's my simple web api.这是我的简单 web api。
public void Post([FromBody] string strJson)
{
SqlConnection cnnCom = null;
SqlCommand cmdCom = null;
string strSql = null;
//write parameter to table
cnnCom = new SqlConnection(CF.Get_Connection_String(CF.DatabaseName.COMMON));
cnnCom.Open();
strSql =
"INSERT INTO Test( " +
"F1) " +
"VALUES( " +
"'" + strJson + "')";
cmdCom = new SqlCommand(strSql, cnnCom);
cmdCom.ExecuteNonQuery();
cnnCom.Dispose();
}
Here is the working c# call to the web api:这是对 web api 的工作 c# 调用:
static void Main(string[] args)
{
HttpClient hc = new HttpClient();
Task<HttpResponseMessage> task1 = null;
//call web api
task1 = hc.PostAsJsonAsync<string>
("http://10.5.72.172:108/api/vbaemail", "abc");
}
http.Open "POST", "http://10.5.72.172:108/api/vbaemail", True
而是 True 设置 False
I was able to get it to work by surrounding the string with single quotes.我能够通过用单引号将字符串括起来来让它工作。 The problem is I can't use single quotes anywhere in the body being passed to the web api, although there's probably a way to escape them.问题是我不能在传递给 web api 的正文中的任何地方使用单引号,尽管可能有一种方法可以将它们转义。 In our case it's not a big deal, we're just trying to patch some old vba programs that are going away eventually.在我们的例子中,这没什么大不了的,我们只是想修补一些最终会消失的旧 vba 程序。
Sub Test()
Dim http As New MSXML2.XMLHTTP60
http.Open "POST", "http://10.5.72.172:108/api/vbaemail", True
http.SetRequestHeader "Content-type", "application/json"
Call http.send("'" + strJson + "'")
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.