PATCH / Post 经典卷曲 ASP

PATCH / Post with curl in Classic ASP

我被扔进了一个相当古老的项目,它是用经典 ASP 制作的。为了我们的需要,我需要做一个简单的 curl-request,更新一些数据。

我是 ASP 的新手,所以我寻找了类似的问题。我偶然发现了这个 问题在这里:

我尽可能地适应了,但似乎我错过了一件重要的事情,在这里我需要你的帮助:

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")。 现在它确实打印了 "updated",所以我想我正在检索 200,但是字段没有更新。

关于uberall API,他们需要一个location-object,应该是这个,目前在我的data-variable中。 (通过 JSON-validator 检查它)。

为了更好的可读性,我也会提供缩进的代码,可能这里有错误:

{  
   "location":{  
      "openingHours":[  
         {  
            "dayOfWeek":1,
            "from1":"07:01",
            "to1":"07:02"
         }
      ]
   }
}

ID 是正确的,我已经仔细检查过了。也许有效载荷是错误的?可能是什么问题?也许需要提供 data 而不是这种方法?

查看 Uberall Tutorials Page

上的示例

看起来好像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)