Carmax
Carmax

Reputation: 2917

Any Idea Why This HTTP Request is Posting Twice?

When I call the function below, it response writes once as expected (last line of function).

But on the api logger of the website being posted to, it shows two posts. Not only that, but the first post is missing the authentication header.

Would somebody be kind enough to look over this code and tell me if I'm doing anything daft?

private function PostToWebsite(data, url)
    Dim  httpRequest, postResponse
    Set httpRequest = Server.CreateObject("MSXML2.ServerXMLHTTP")
    httpRequest.Open "POST", url, False, "un", "pw"
    httpRequest.SetRequestHeader "Content-Type", "application/json"     
    httpRequest.setRequestHeader "Content-Length", len(data)
    httpRequest.Send data
    if httpRequest.status = 201 then    
        PostToWebsite = "ok/" & httpRequest.getResponseHeader("Location")
    elseif httpRequest.status = 400 then
        PostToWebsite= "error/Http 400 error: " & httpRequest.responseText 
    elseif httpRequest.status = 401 then 
        PostToWebsite= "error/Http 401 error: " & httpRequest.responseText 
    else    
        PostToWebsite= "error/Unknown status in PostToWebsite"
    end if
    Set httpRequest = nothing

    RESPONSE.WRITE  PostToWebsite   'this line writes only once

end function

Upvotes: 1

Views: 292

Answers (1)

Carmax
Carmax

Reputation: 2917

It turns out that there was a comma missing from the JSON payload. Once I fixed that it worked fine.

My new question is: why on earth would that generate a double post, as opposed to a single one that failed?!

Upvotes: 1

Related Questions