我被扔进了一个非常古老的项目,它是用经典的ASP制作的。为了满足我们的需求,我需要做一个简单的curl
- 请求,以更新一些数据。
我是ASP的新手,所以我找了类似的问题。我偶然发现了这个 问题在这里:
How can I post data using cURL in asp classic?
我试图尽可能地适应,但似乎我错过了一件重要的事情,在这里我需要你的帮助:
functions.asp
public function makeCurlRequest(strMethod)
Dim http: Set http = Server.CreateObject("WinHttp.WinHttpRequest.5.1")
Dim privateKey
privateKey = "abc def"
Dim url: url = "https://sandbox.uberall.com/api/locations/322427?private_key=" & privateKey
Dim data: data = "{""location"":{""openingHours"":[{""dayOfWeek"":1,""from1"":""07:01"",""to1"":""07:02""}]}}"
'method needs to be PATCH
With http
Call .Open(strMethod, url, False)
Call .SetRequestHeader("Content-Type", "application/json")
Call .Send(data)
End With
If Left(http.Status, 1) = 2 Then
response.write("updated")
response.end()
Else
'Output error
Call Response.Write("Server returned: " & http.Status & " " & http.StatusText)
End If
end function
在我的档案中,我只是致电makeCurlRequest("PATCH")
。
现在确实打印了#34;更新了#34;所以我想我正在检索200
,但字段没有更新。
关于uberall API,它们需要一个location
- 对象,应该是这个,当前在data
- 变量中。 (通过JSON验证器检查)。
为了更好的可读性,我还提供了缩进代码,也许这里有一个错误:
{
"location":{
"openingHours":[
{
"dayOfWeek":1,
"from1":"07:01",
"to1":"07:02"
}
]
}
}
ID是正确的,我已经仔细检查过了。也许有效载荷是错的?可能是什么问题?可能需要提供data
而不是这种方法吗?
答案 0 :(得分:1)
看起来好像不需要location
对象的封装,而是构造像
{
"openingHours":[
{
"dayOfWeek":1,
"from1":"07:01",
"to1":"07:02"
}
]
}
在代码中,将data
变量更改为;
Dim data: data = "{""openingHours"":[{""dayOfWeek"":1,""from1"":""07:01"",""to1"":""07:02""}]}"
必须承认我必须在文档中挖掘一下,找到一个示例,说明他们如何期望结构化请求的主体,这对于API来说并不是很好。此外,如果有效负载是错误的,您应该收回错误,因此您知道有效负载存在问题,HTTP 400 Bad Request
的某些内容是有意义的。
API也可能使用HTTP 200 OK,在这种情况下可能会错过任何错误,所以在测试时你可以做这样的事情;
Dim http: Set http = Server.CreateObject("WinHttp.WinHttpRequest.5.1")
Dim privateKey
privateKey = "abc def"
Dim url: url = "https://sandbox.uberall.com/api/locations/322427?private_key=" & privateKey
'Purposefully passing the wrong structure to see what is returned.
Dim data: data = "{""location"":{""openingHours"":[{""dayOfWeek"":1,""from1"":""07:01"",""to1"":""07:02""}]}}"
'method needs to be PATCH
With http
Call .Open(strMethod, url, False)
Call .SetRequestHeader("Content-Type", "application/json")
Call .Send(data)
End With
'Not bothered about the status for now, just give me the response.
Call Response.Write("Server returned: " & http.Status & " " & http.StatusText)
Call Response.Write("Body: " & http.ResponseText)