Author Topic: Trying to POST data to webpage using WinHTTP.WinHTTPRequest.5.1  (Read 1628 times)

0 Members and 1 Guest are viewing this topic.

LogicTools

  • Newt
  • Posts: 23
Trying to POST data to webpage using WinHTTP.WinHTTPRequest.5.1
« on: September 22, 2021, 02:29:02 PM »
I am trying to save basic data, names, email addresses, to a webpage using WinHTTP.WinHTTPRequest.5.1 but nothing is written to the page.
To test I am working with a local server set for me with the help of mi son.
On the screen captures of the cmd and browser windows in none of them looks that any info is written.
Is it the way I am passing the data parameters to the POST method, the way the server is set or could for any other reason?
There are examples of retrieving data with the GET method, but I could not see anything related the saving of data with the POST method.
I am pretty new to the use of this object. Any help will be highly appreciated. 
Code - Auto/Visual Lisp: [Select]
  1. (defun MyWinHTTPRequest  (/ web_page list_data list_type array_data win_object)
  2.  (setq
  3.   web_page   "http://localhost:4001/test"
  4.   list_data  '("username" "billythekid")
  5.   list_type  (vlax-make-safearray vlax-vbstring '(0 . 1))
  6.   array_data (vlax-safearray-fill list_type list_data)
  7.   win_object (vlax-create-object "WinHTTP.WinHTTPRequest.5.1")
  8.   )
  9.  (vlax-invoke-method win_object 'open "POST" web_page :vlax-false)
  10.  (vlax-invoke-method win_object 'send array_data)
  11.  (if win_object (vlax-release-object win_object))
  12.  (princ)
  13.  )

CMD window and webpage

John Kaul (Se7en)

  • Administrator
  • Needs a day job
  • Posts: 9769
Re: Trying to POST data to webpage using WinHTTP.WinHTTPRequest.5.1
« Reply #1 on: September 22, 2021, 03:46:35 PM »
I was going to start playing with some GET and POST stuff in C just the other day so I've only started reading up on the subject but from what I've gleaned so far, you do not have enough information for a POST. You also need the content length.

It looks like you have the other data there (-i.e. the location and the port; assuming the "WinHTTPRequest" method handles all the socket stuff). But I notice that you are not waiting for the response, you just close.
TheSwamp.org (serving the CAD community since 2003)

Donate to TheSwamp.org

LogicTools

  • Newt
  • Posts: 23
Re: Trying to POST data to webpage using WinHTTP.WinHTTPRequest.5.1
« Reply #2 on: September 22, 2021, 04:11:36 PM »
Thank you John. I am very green on this http staff. You can see that I just did a very basic code to test if it was working.
I can see a WaitForResponse method, but I don't know how I have to deal with the content length and what is the information I am missing for the POST.
I will keep trying.
Thanks again.

MickD

  • King Gator
  • Posts: 3513
  • (x-in)->[process]->(y-out)
Re: Trying to POST data to webpage using WinHTTP.WinHTTPRequest.5.1
« Reply #3 on: September 22, 2021, 05:39:17 PM »
You need to define the header section at the very least, you will then need to add the data in the format required, see:
https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST
There's a lot of good info there, have a look through there and then some blog posts etc to get a better feel for what's going on.

If it's an API to another company's data you may also need to define extra header info for user and password etc but their API doc's should detail how to do this.

hth, cheers
Forth is like the Tao: it is a Way, and is realized when followed.
Its fragility is its strength; its simplicity is its direction - Michael Ham

"First, solve the problem. Then, write the code." John Johnson

LogicTools

  • Newt
  • Posts: 23
Re: Trying to POST data to webpage using WinHTTP.WinHTTPRequest.5.1
« Reply #4 on: September 22, 2021, 07:43:35 PM »
Thanks Mickd.
There is a lot of research to do for a lisper like me to implement this kind of functions.
It also will take me while to digest all that info that you have suggested.
I am even wondering if I am using the right object for the job.

MickD

  • King Gator
  • Posts: 3513
  • (x-in)->[process]->(y-out)
Re: Trying to POST data to webpage using WinHTTP.WinHTTPRequest.5.1
« Reply #5 on: September 22, 2021, 09:28:22 PM »
No problems.
What you are doing is writing to a RESTful API or REST for short. Normally a web browser handles building the headers etc but with REST you need to do this, it's not too hard really, do one you've done them all just about :)

With REST you just send/receive data without all the html etc to build a web page. Think Command Line Interface as opposed to Graphical User Interface.

Forth is like the Tao: it is a Way, and is realized when followed.
Its fragility is its strength; its simplicity is its direction - Michael Ham

"First, solve the problem. Then, write the code." John Johnson

LogicTools

  • Newt
  • Posts: 23
Re: Trying to POST data to webpage using WinHTTP.WinHTTPRequest.5.1
« Reply #6 on: September 24, 2021, 01:25:04 PM »
Thanks again MickD.
I guess that after reading about REST and HTTP Requests etc... I think I should rephrase my call for help.

First: What would be the correct object to use:
Msxml2.ServerXMLHTTP WinHTTP.WinHTTPRequest.5.1 MSXML2.XMLHTTP.3.0  MSXML2.XMLHTTP or any other?

Second: If my data is: username = BillytheKid, email = BillytheKid@BillytheKid.com
In autolisp what will be the type and format for the variable to enter at the "POST" "send" method call?

A string like this?
Code - Auto/Visual Lisp: [Select]
  1. "username=BillytheKid&email=BillytheKid@BillytheKid.com"
A list like this?
Code - Auto/Visual Lisp: [Select]
  1. (list "username" "BillytheKid" "email" "BillytheKid@BillytheKid.com")

A dotted pair list
Code - Auto/Visual Lisp: [Select]
  1. (list ("username" . "BillytheKid") ("email" . "BillytheKid@BillytheKid.com"))

Or any other type of data?

VovKa

  • Swamp Rat
  • Posts: 1383
  • Ukraine
Re: Trying to POST data to webpage using WinHTTP.WinHTTPRequest.5.1
« Reply #7 on: September 24, 2021, 02:43:52 PM »
is it necessary to use POST?
there are a lot of examples of GET requests
just search theswamp for "WinHTTP.WinHTTPRequest.5.1"

LogicTools

  • Newt
  • Posts: 23
Re: Trying to POST data to webpage using WinHTTP.WinHTTPRequest.5.1
« Reply #8 on: September 24, 2021, 02:55:57 PM »
Do I need POST or GET to put the information in the server?

MickD

  • King Gator
  • Posts: 3513
  • (x-in)->[process]->(y-out)
Re: Trying to POST data to webpage using WinHTTP.WinHTTPRequest.5.1
« Reply #9 on: September 24, 2021, 05:56:45 PM »
The data format really depends on the library you are using, by the looks from this tutorial I found it looks like you need to build the first string but I only quickly browsed the article but there should be enough to get an idea.
Most of the info for that lib is for VB but the calls and parameters are the same and should be easily built with lisp also.

The library you use again depends on how much data you want to send and how the endpoint expects to receive it, that is, if the endpoint API expects XML then you will need to use the XML version. You don't have to but it will be much easier to construct the request properly.

hth

https://learn.objectiflune.com/howto/scripts-communicate-https-api-worfklow/

https://docs.microsoft.com/en-us/windows/win32/winhttp/winhttprequest
Forth is like the Tao: it is a Way, and is realized when followed.
Its fragility is its strength; its simplicity is its direction - Michael Ham

"First, solve the problem. Then, write the code." John Johnson

LogicTools

  • Newt
  • Posts: 23
Re: Trying to POST data to webpage using WinHTTP.WinHTTPRequest.5.1
« Reply #10 on: September 25, 2021, 12:16:13 PM »
Thank you MickD for your guidance. As I said before this web thing is totally new for me and is not only the lisp part of it, but also the web server my son is working on.
Pending on some changes that my son is doing on the server, following your indications I came up with this code:
Code - Auto/Visual Lisp: [Select]
  1. (defun MyHTTPRequest  (/ url data http_object)
  2.  (setq
  3.   url   "http://localhost:4001/test"
  4.   data "username=billythekid&email=thekid@billythekid.com"
  5.   http_object (vlax-create-object "MSXML2.XMLHTTP.3.0")
  6.   )
  7.  (vlax-invoke-method http_object 'open "POST" url :vlax-false)
  8.  (vlax-invoke-method http_object 'SetRequestHeader "Content-type" "application/x-www-form-urlencoded")
  9.  (vlax-invoke-method http_object 'SetRequestHeader "Content-length" (strlen data))
  10.  (vlax-invoke-method http_object 'send data)
  11.  (print (vlax-get-property http_object "ResponseText"))
  12.  (if http_object (vlax-release-object http_object))
  13.  (princ)
  14.  )

Am I on the write track?
This is using the MSXML2.XMLHTTP.3.0 object. My question again is: Is this the better choice for this job?

The ResponseText is "{}"
I will post the results when the modifications on the web server are done.
Thanks again

MickD

  • King Gator
  • Posts: 3513
  • (x-in)->[process]->(y-out)
Re: Trying to POST data to webpage using WinHTTP.WinHTTPRequest.5.1
« Reply #11 on: September 25, 2021, 06:10:12 PM »
Pretty close I think. I haven't dug into the doc's too much but based on the sample code below from the link I gave you this is my rough interpretation into lisp.
Note that you are sending a simple http data string not XML so I changed it back to the Http API object. If you want to send XML you will need to have the data as an XML string.
To receive the ResponseText the endpoint on the server needs to handle this request properly and send it back in the correct format.
Also, you need to make sure the endpoint URL is correct, for example, is it handled by a routing library that can give you clean url's like you have or does it need the filetype (such as test.php)?

Basically you are sending a chunk of data as a string that is broken into the header section which describes the payload (text, xml, json, length etc and other meta data as required) and the payload itself. Depending on the library you are using dictates how much of the header details etc are required to be encoded by hand, this is where you need to study the doc's a bit and look at some samples.
Cheers.

Code - Auto/Visual Lisp: [Select]
  1. (defun MyHTTPRequest  (/ url data http_object)
  2.  (setq
  3.   url   "http://localhost:4001/test"
  4.   data "username=billythekid&email=thekid@billythekid.com" ;; this data is not XML, it's a plain http type of data string
  5.   http_object (vlax-create-object "WinHTTP.WinHTTPRequest.5.1") ;; so we need to use this, not the XML lib
  6.   )
  7.  (vlax-invoke-method http_object 'open "POST" url :vlax-false)
  8.  (vlax-invoke-method http_object 'SetRequestHeader "Content-type" "application/x-www-form-urlencoded")
  9.  ;; not sure about below, maybe API handles string length for you??
  10.  ;; (vlax-invoke-method http_object 'SetRequestHeader "Content-length" (strlen data))
  11.  (vlax-invoke-method http_object 'send data)
  12.  
  13.  (print (vlax-get-property http_object "ResponseText"))
  14.  (if http_object (vlax-release-object http_object))
  15.  (princ)
  16.  )
  17.  

<edit>A quick search put up these results, worth a look through and the first one has a working example in Autolisp, cheers.
https://www.google.com/search?q=autolisp+send+http+request&rlz=1C1CHBF_en-GBAU929AU929&oq=autolisp+send+http+request&aqs=chrome..69i57j33i160.5853j0j4&sourceid=chrome&ie=UTF-8
« Last Edit: September 25, 2021, 06:13:35 PM by MickD »
Forth is like the Tao: it is a Way, and is realized when followed.
Its fragility is its strength; its simplicity is its direction - Michael Ham

"First, solve the problem. Then, write the code." John Johnson

LogicTools

  • Newt
  • Posts: 23
Re: Trying to POST data to webpage using WinHTTP.WinHTTPRequest.5.1
« Reply #12 on: September 28, 2021, 10:26:20 AM »
Thanks for your guidance again MickD.
As soon as my son sets the server and we try the code I will keep you posted with the results.

LogicTools

  • Newt
  • Posts: 23
Re: Trying to POST data to webpage using WinHTTP.WinHTTPRequest.5.1
« Reply #13 on: October 17, 2021, 10:07:38 AM »
Thank you MickD for your guidance and sorry I didn't come back to you earlier, my son and I we live in different continents and has been difficult to be able to coordinate a time table.
After he created a new server and following your indications everything works as it should, the info that is passed to the server is received and saved in it.
Thanks again for helping to solve my problem.

MickD

  • King Gator
  • Posts: 3513
  • (x-in)->[process]->(y-out)
Re: Trying to POST data to webpage using WinHTTP.WinHTTPRequest.5.1
« Reply #14 on: October 17, 2021, 04:51:43 PM »
Not a problem, all good things take time :)
Glad you got it working, it opens up many new possibilities for you! cheers.
Forth is like the Tao: it is a Way, and is realized when followed.
Its fragility is its strength; its simplicity is its direction - Michael Ham

"First, solve the problem. Then, write the code." John Johnson