No announcement yet.

POST JSON data to https website

  • Filter
  • Time
  • Show
Clear All
new posts

  • POST JSON data to https website

    I have no experience programming the Internet ...

    I need to POST to a website JSON data and receive other JSON data as result.

    I'm using this code, created collecting pieces from many old discussions in this forum.

    function PBMAIN() as long
    local iReturn as long
    local iRead as dword
    local hInternet as dword
    local hConn as dword
    local hRequest as dword
    local sHeaders as asciiz*1200
    local sBuffer as string
    local ErrMsg as string
    local sReturnData as string
    local sMyUrl as asciiz*%MAX_PATH
    local sMethod as asciiz*%MAX_PATH
    local sObjName as asciiz*%MAX_PATH
    local sVersion as asciiz*%MAX_PATH
    local sBody as string
    hInternet = InternetOpen(byval %NULL, %INTERNET_OPEN_TYPE_PRECONFIG, byval %NULL, byval %NULL, 0)
    if hInternet = 0 then
       ErrMsg = "InternetOpen failed"
       exit function
    end if
    sMyUrl = ""
    hConn = InternetConnect(hInternet,sMyUrl,%INTERNET_DEFAULT_HTTPS_PORT,"", "", %INTERNET_SERVICE_HTTP, 0, 0)
    if hConn = 0 then
       ErrMsg = "InternetConnect failed"
       InternetCloseHandle hInternet
       exit function
    end if
    hRequest = httpOpenRequest(hConn, sMethod, sObjName, sVersion, byval %NULL, byval %NULL, %INTERNET_FLAG_RELOAD OR %INTERNET_FLAG_NO_CACHE_WRITE OR %INTERNET_FLAG_PRAGMA_NOCACHE,0)
    if hRequest = 0 then
       ErrMsg = "httpOpenRequest failed."
       InternetCloseHandle hConn
       InternetCloseHandle hInternet  
       msgbox ErrMsg
       exit function
    end if
    sBody = "{|"+ _
    " 'username': 'wrongusername', |" + _
    " 'password': 'wrongpassword' |"+ _
    replace "'" with $dq in sBody
    replace "|" with $crlf in sBody
    sHeaders = "Host:" + $crlf + _
    "Content-Type: application/json" + $crlf + _
    "Content-Length: "+FORMAT$(len(sBody)) + $crlf
    if httpSendRequest(hRequest, sHeaders, len(sHeaders), byval strptr(sBody), len(sBody)) = 0 then
       ErrMsg = "httpSendRequest failed."
       InternetCloseHandle hRequest
       InternetCloseHandle hConn
       InternetCloseHandle hInternet
       exit function
    end if
    sReturnData = ""
       iRead = 0
       sBuffer = space$(%HTTP_BUFFER_LENGTH)
       iReturn = InternetReadFile(hRequest, byval strptr(sBuffer), %HTTP_BUFFER_LENGTH, iRead)
       if iRead > 0 then
          sReturnData = sReturnData + left$(sBuffer, iRead)
          if iReturn <> 0 then
             exit do
          end if
       end if
    msgbox sReturnData
    end function
    It doesn't work; I always receive an html page with the error 400 Bad request.

    <head><title>400 Bad Request</title></head>
    <center><h1>400 Bad Request</h1></center>
    What's going wrong?

    Please, note that user and passord in the JSON body are wrong (I still don't have the right one), but all other elements are real.
    Using this POST in PostMan I receive a JSON message saying access is denied because username and password are wrong (of course).

    If could be useful, this Powershell script (generated with PostMan) seems to work correctly, even if I don't understand why it seems to treat the result as an error ...

    $headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
    $headers.Add("Content-Type", "application/json")
    $body = "{`n `"username`": `"yourusername`",`n `"password`": `"yourpassword`"`n}"
    $response = Invoke-RestMethod '' -Method 'POST' -Headers $headers -Body $body
    $response | ConvertTo-Json
    Here the result

    Invoke-RestMethod : {

    "message": "Access denied - Please check that your username and password are correct. Please be aware

    that usernames and passwords are case sensitive. If the problem persists, please contact your Creditsafe

    account manager."


    In testpost.ps1:6 car:13
    + $response = Invoke-RestMethod ' ...
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand
    Thank you for any help!

  • #2
    The WinHttp functions may be what you are looking for:

    or using COM
    How long is an idea?
    Write it down.


    • #3
      Thank you, I'll try it.


      • #4
        Tried with WinHttp, same result: 400 Bad request.
        With some different combination of parameters I got some different results (404 Not found and others), perhaps that's the right way, but I was unable to get it working.

        I solved with cURL, stand-alone utility that I can invoke via shell after preparing text files with headers and body.
        It's easy, just do a shell to a command like this:

        cUrl --no-progress-meter --output output.txt --request POST --header @header.txt --data-binary @body.txt
        The same way to send GET method ...