Search code examples
restcurlvbscriptasp-classic

PATCH / Post with curl in Classic ASP


I was thrown into a pretty old project, which is made in classic ASP. For our needs, I need to make a simple curl-request, to update some data.

I'm pretty new to ASP, so I looked for similar problems. I stumbled upon this question here:

How can I post data using cURL in asp classic?

I tried to adapt as much as possible, but it seems like I'm missing an important thing and here I need your help:

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

In my file, I simply call makeCurlRequest("PATCH"). Now it does indeed print "updated", so I guess I'm retrieving a 200, but the fields aren't updated.

Regarding to the uberall API, they require a location-object, which should be this, what is currently in my data-variable. (Checked it via a JSON-validator).

For a better readability, I'll provide the indented code as well, maybe here is an error:

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

The ID's are correct, I double-checked that already. Maybe the payload is wrong? What might be the problem? Maybe data needs to be provided otherwise instead of this approach?


Solution

  • Looking at the examples on Uberall Tutorials Page

    It looks as though the encapsulation of the location object is not necessary, instead structure the body like

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

    In the code, change the data variable to be;

    Dim data: data = "{""openingHours"":[{""dayOfWeek"":1,""from1"":""07:01"",""to1"":""07:02""}]}"
    

    Must admit I had to dig around in the documentation to find an example that showed how they expected the body of the request to be structured, which isn't great for an API. Also if the payload was wrong you should be getting back an error so you know there was a problem with the payload, something along the lines of HTTP 400 Bad Request would make sense.

    It's also possibly that the API uses HTTP 200 OK for everything, in which case any errors might get missed, so while testing you could just do something like this;

    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)