SIM
SIM

Reputation: 22440

POST request created in vba brings back nothing as result

I've written a very tiny script in vba using POST request. However, when I run it, I get nothing as result except for a blank message. I've tried to fill in the request parameter accordingly. Perhaps, I can't notice which should be included in the parameter. The page I'm dealing with contains several images in it's right panel. When an image is clicked the request about which i'm talking here is sent to the server and brings back the result and displays new information concerning its' flavor under it. My goal is to parse all the flavors connected to each images. Anyways, I'm trying to attach all the things necessary to find out what i'm missing. Thanks in advance.

This is what I got from chrome developer tools to prepare the POST request: "https://www.dropbox.com/s/zjn0ahixhu58miq/RequestStatus.txt?dl=0"

Here is what I'm trying with:

Sub PostReq()

    Dim http As New XMLHTTP60, html As New HTMLDocument
    Dim ArgumentStr As String

    ArgumentStr = "opt=flavor&opt1=207&opt2=47&ip=105"
    With http
        .Open "POST", "https://www.optigura.com/product/ajax/details.php", False
        .setRequestHeader "User-Agent", "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36"
        .setRequestHeader "Content-type", "application/x-www-form-urlencoded"
        .setRequestHeader "Accept", "application/json, text/javascript, */*; q=0.01"
        .send ArgumentStr
        html.body.innerHTML = .responseText
    End With

    MsgBox http.responseText

End Sub

This is the original link to the webpage:

"https://www.optigura.com/uk/product/gold-standard-100-whey/"

Upvotes: 0

Views: 283

Answers (2)

SIM
SIM

Reputation: 22440

Finally, I've made it. To receive the required response it is necessary to send a GET request first then again send a POST request using the response from that get request. Here is the working one:

Sub httpPost()

    Dim http As New XMLHTTP60, html As New HTMLDocument
    Dim ArgumentStr As String

    ArgumentStr = "opt=flavor&opt1=207&opt2=47&ip=105"

    With http
        .Open "GET", "https://www.optigura.com/uk/product/gold-standard-100-whey/", False
        .send
    End With

    With http
        .Open "POST", "https://www.optigura.com/product/ajax/details.php", False
        .setRequestHeader "X-Requested-With", "XMLHttpRequest"
        .setRequestHeader "Content-type", "application/x-www-form-urlencoded"
        .setRequestHeader "Accept", "application/json, text/javascript, */*; q=0.01"
        .send ArgumentStr
        html.body.innerHTML = .responseText
    End With

    MsgBox http.responseText

End Sub

Upvotes: 0

SierraOscar
SierraOscar

Reputation: 17637

Your code sets a request header like so:

.setRequestHeader "Content-type", "application/x-www-form-urlencoded"

so the script is going to expect the argument string to be URL encoded (which it isn't).

Try either encoding the string, or send the request using "GET" instead.

Upvotes: 1

Related Questions