Jump to content

How can I retrieve the JSON data from a web API


Fliwatt
 Share

Recommended Posts

Hello guys,
this is my first time working with a web API and trying to navigate that seems really hard, right now I am stuck in how to fetch the JSON results into my script, the website I am using is really simple, it just displays the gender of a name, it is https://api.genderize.io/?name=peter
I tried using the  WinHTTP function already but it only returns

<html>
<head><title>400 The plain HTTP request was sent to HTTPS port</title></head>
<body bgcolor="white">
<center><h1>400 Bad Request</h1></center>
<center>The plain HTTP request was sent to HTTPS port</center>
<hr><center>nginx/1.10.3 (Ubuntu)</center>
</body>
</html>

This is my code:

#include "WinHttp.au3"

Opt("MustDeclareVars", 1)

; Initialize and get session handle
Global $hOpen = _WinHttpOpen()
; Get connection handle
Global $hConnect = _WinHttpConnect($hOpen, "https://api.genderize.io/?name=peter")
; Request
Global $hRequest = _WinHttpSimpleSendRequest($hConnect)

; Simple-read...
Global $sRead = _WinHttpSimpleReadData($hRequest)
ConsoleWrite( StringLeft($sRead, 1100) & "...")

; Close handles
_WinHttpCloseHandle($hRequest)
_WinHttpCloseHandle($hConnect)
_WinHttpCloseHandle($hOpen)

I think I should not retrieve the data using an HTTP call but what should I do instead?

Thank you very much for your help!

Link to comment
Share on other sites

You're using _WinHttpSimpleSendRequest, which is for HTTP. You'll need to switch to _WinHttpSimpleSendSSLRequest or examine the following simplified example that works --

#include "WinHttp.au3"

Opt("MustDeclareVars", 1)

; Initialize and get session handle
Global $hOpen = _WinHttpOpen()
; Get connection handle
Global $hConnect = _WinHttpConnect($hOpen, "https://api.genderize.io")

Local $sReturned = _WinHttpSimpleSSLRequest($hConnect, "GET", "?name=peter")

ConsoleWrite( StringLeft($sReturned, 1100) & "...")

; Close handles
_WinHttpCloseHandle($hConnect)
_WinHttpCloseHandle($hOpen)

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...