Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.9k views
in Technique[技术] by (71.8m points)

vbscript - How can I post data using cURL in asp classic?

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?

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...