Help with HTTPsocket.Post

I Know next to nothing about using the HTTPSocket and net stuff in general I am having hard time figuring out how to retrieve information from a a publically accessible database. After reading the instrctions on teh website and the HTTPSocket doc page I tried to do it myself but I get an error and am not sure what to try next

I’m hoping someone can show me how to do this is xojo the classic HTTPScoket

The instruction they give are:

[quote]There are existing standards for just how the information in the POST body is formatted, and you must include in the PUG REST call an HTTP header that indicates which content type you are supplying. The simpler format is “Content-Type: application/x-www-form-urlencoded” which is the same as the URL argument syntax after the ‘?’ separator, of the general form “arg1=value1&arg2=value2&…” (See here for more technical detail on these content types.) For example, use a URL like

https://pubchem.ncbi.nlm.nih.gov/rest/pug/compound/inchi/cids/JSON

with “Content-Type: application/x-www-form-urlencoded” in the request header, and put the string

inchi=InChI=1S/C3H8/c1-3-2/h3H2,1-2H3

in the POST body.[/quote]

I tried the following code:

Dim Socket As New HTTPSocket Socket.SetRequestContent( "inchi=InChI=1S/C3H8/c1-3-2/h3H2,1-2H3", "application/x-www-form-urlencoded") dim S as String = Socket.Post("https://pubchem.ncbi.nlm.nih.gov/rest/pug/compound/inchi/cids/JSON",30) Dim C as New Clipboard C.Text = S c.Close

And what I got back was:

[code]

NCBI - WWW Error 403 Diagnostic ' I cut out the rest [/code]

I would greatly appreciate it if someone could show me how to do this correctly…

Thanks,

  • karen

Well I made little progress…

I changed my code to:

[code]Dim Socket As New HTTPSecureSocket
Socket.SetRequestContent( EncodeURLComponent(“inchi=InChI=1S/C3H8/c1-3-2/h3H2,1-2H3”), “application/x-www-form-urlencoded”)

dim S as String = Socket.Post(“https://pubchem.ncbi.nlm.nih.gov/rest/pug/compound/inchi/cids/JSON”,30)

Dim C as New Clipboard
C.Text = S
c.Close
[/code]

And got:

[quote]Status: 400
Code: PUGREST.BadRequest
Message: Missing structure string
Detail: An argument for ‘inchi’ must be provided in the URL path or POST data
[/quote]

Obviously I am not setting the post data correctly, but I have no idea on how to do it right.

  • karen

The old HTTPSocket has a .Post method that you would use instead of the .Get

If you don’t care about your querystring (the part after the ?) ending up in their log files then you can get it with a Get using:

Dim Socket As New HTTPSocket 'Socket.SetRequestContent( EncodeURLComponent("inchi=InChI=1S/C3H8/c1-3-2/h3H2,1-2H3") , "application/x-www-form-urlencoded") Dim S As String = Socket.Get("https://pubchem.ncbi.nlm.nih.gov/rest/pug/compound/inchi/cids/JSON?inchi=InChI=1S/C3H8/c1-3-2/h3H2,1-2H3",30) Dim C As New Clipboard C.Text = S c.Close

Thanks… But using Post I get the same response… Changed my previous post to reflect that

  • Karen

[quote=362804:@JulianS]Dim S As String = Socket.Get(“https://pubchem.ncbi.nlm.nih.gov/rest/pug/compound/inchi/cids/JSON?inchi=InChI=1S/C3H8/c1-3-2/h3H2,1-2H3”,30)
[/quote]

One question…does length matter?

The InChI (International Chemical Identifier) string can be a LOT longer than in this example… Which I think is why they recommend using Post for this.

Thanks.

  • karen

2048 chars in IE is about the shortest out of all the browsers and servers

Give me a few mins and I’ll try and get the post working, not done this with xojo before so I’m just poking around at the moment.

[quote=362807:@JulianS]2048 chars in IE is about the shortest out of all the browsers and servers

Give me a few mins and I’ll try and get the post working, not done this with xojo before so I’m just poking around at the moment.[/quote]

Thanks!

  • karen

I looked up the docs because this is slightly confusing. It turns out this is a URL-encoded form (not a URL query string) with one element named inchi with a value of InChI=1S/C3H8/c1-3-2/h3H2,1-2H3. Use a Dictionary to build the form and then use SetFormData to assign it to the socket:

Dim h As New HTTPSocket Dim form As New Dictionary form.Value("inchi") = "InChI=1S/C3H8/c1-3-2/h3H2,1-2H3" h.SetFormData(form) Dim result As String = h.Post("https://pubchem.ncbi.nlm.nih.gov/rest/pug/compound/inchi/cids/JSON", 0)

i notice your using HTTPSocket instead of HTTPSecureSocket.
as the post url is “https://”

[quote=362850:@Derk Jochems]i notice your using HTTPSocket instead of HTTPSecureSocket.
as the post url is “https://”[/quote]

I actually had to use HTTPSecureSocket to get it to work

  • Karen