5

How can I post data from order.asp to 3rd party url?

I have all parameters in form tag.

On submission 3rd party want me to add two values as header. 3rd party code is as below

curl https://www.instamojo.com/api/1.1/payment-requests/ \
  --header "X-Api-Key: [API_KEY]" \
  --header "X-Auth-Token: [AUTH_TOKEN]" \
  --data     
 "allow_repeated_payments=False&amount=2500&buyer_name=John+Doe&purpose=FIFA+16&redirect_url=http%3A%2F%2Fwww.example.com%2Fredirect%2F&phone=9999999999&send_email=True&webhook=http%3A%2F%2Fwww.example.com%2Fwebhook%2F&send_sms=True&email=foo%40example.com"

I am using asp classic. Can I use response.AddHeader name,value to pass both values X-Api-Key and X-Auth-Token?

If not possible, then how to use curl in asp classic?

user692942
  • 16,398
  • 7
  • 76
  • 175
lokesh purohit
  • 179
  • 2
  • 2
  • 16

1 Answers1

12

You can do this using the WinHttpRequest object

<%
Dim http: Set http = Server.CreateObject("WinHttp.WinHttpRequest.5.1")
Dim url: url = "https://www.instamojo.com/api/1.1/payment-requests/"
Dim data: data = "allow_repeated_payments=False&amount=2500&buyer_name=John+Doe&purpose=FIFA+16&redirect_url=http%3A%2F%2Fwww.example.com%2Fredirect%2F&phone=9999999999&send_email=True&webhook=http%3A%2F%2Fwww.example.com%2Fwebhook%2F&send_sms=True&email=foo%40example.com"

With http
  Call .Open("POST", url, False)
  Call .SetRequestHeader("Content-Type", "application/x-www-form-urlencoded")
  Call .SetRequestHeader("X-Api-Key", "yourvalue")
  Call .SetRequestHeader("X-Auth-Token", "yourvalue")
  Call .Send(data)
End With

If Left(http.Status, 1) = 2 Then
  'Request succeeded with a HTTP 2xx response, do something...
Else
  'Output error
  Call Response.Write("Server returned: " & http.Status & " " & http.StatusText)
End If
%>

This is just a hard-coded example, usually you would build the data variable via some method rather then passing a hard-coded string.

What about Response.AddHeader()?

Response.AddHeader() is used in Classic ASP to set HTTP headers being returned to the client when the server is sending a response.

In this scenario the ASP page is the client sending a request to another server so in this context you wouldn't use Response.AddHeader but the SetRequestHeader() method of the WinHttpRequest object instead.

Community
  • 1
  • 1
user692942
  • 16,398
  • 7
  • 76
  • 175
  • i have passed values in data variable what i have retrieved as request method like <% amount=" & amount & "&buyer_name=" & buyer_name & "%>. but its showing error. one more issue. with submission data, should be jump to 3rd party's page for payment. how can v do? – lokesh purohit May 27 '16 at 04:53
  • @Lanymart, the error is "Server returned: 415 UNSUPPORTED MEDIA TYPE" – lokesh purohit May 27 '16 at 05:03
  • @lokeshpurohit it will be the `Content-Type` header, default for `POST` should be `application/x-www-form-urlencoded`, updated answer. – user692942 May 27 '16 at 07:31
  • in your code its return just status on same page. i want to jump to 3rd party for payment process while submit. how to do it? – lokesh purohit May 27 '16 at 08:43
  • 2
    @lokeshpurohit At least I've written some code *(glad to see the addition of `Content-Type` worked, your welcome by the way)*. This code is just an example, client <-> server interaction, does `curl` redirect to a 3rd party for payment? I'm guessing not. You can take this code and build on it as a starting point. Plus if it's returning `Server returned: ....` then you are not getting a valid response, that is there to detect when you don't receive a `HTTP 200 OK` response, so maybe pull back `ResponseText` to see what's happening?? – user692942 May 27 '16 at 08:48
  • 2
    @lokeshpurohit Either way I've answered the original question. See [What should I do when someone answers my question?](http://stackoverflow.com/help/someone-answers). – user692942 May 27 '16 at 08:53
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/113121/discussion-between-lokesh-purohit-and-lankymart). – lokesh purohit May 27 '16 at 08:59