JSON Post and Get sample

JSON Post and Get sample

Postby cdmmaui » Fri Jan 06, 2017 9:06 pm

Hello,

Does anyone have a JSON example that includes authentication.

I tried the following but got the error listed below:

//////////////////////////////////////////////////////////////// CODE

#include "FiveWin.ch"

REQUEST DBFCDX
REQUEST DBFFPT

//-----------------------------------------------------------------------------
function Main()

local ohttp := CreateObject( "MSXML2.XMLHTTP" )
local strret

ohttp:Open( "GET" ,"https://www.winwebconnect.com/api/v1/Login", .F. )
ohttp:SetRequestHeader( "Accept" , "application/json" )
ohttp:SetRequestHeader( "Content-Type" , "application/json" )
ohttp:SetRequestHeader( "Username" , "xxx" )
ohttp:SetRequestHeader( "Password" , "xxx" )

strret := alltrim(ohttp:responseText)

MsgInfo( strret )

RETURN 0

//////////////////////////////////////////////////////////////// REPONSE
Application
===========
Path and name: C:\Winapps\cargo\data\winweb.exe (32 bits)
Size: 3,068,928 bytes
Compiler version: Harbour 3.2.0dev (r1306132159)
FiveWin Version: FWH 14.09
Windows version: 6.1, Build 7601 Service Pack 1

Time from start: 0 hours 0 mins 0 secs
Error occurred at: 01/06/17, 14:55:24
Error description: (DOS Error -2147352567) WINOLE/1007 Unspecified error
(0x80004005): msxml3.dll

Stack Calls
===========
Called from: => TOLEAUTO:RESPONSETEXT( 0 )
Called from: winweb.prg => MAIN( 37 )

System
======
CPU type: Intel(R) Core(TM) i7-6820HQ CPU @ 2.70GHz 2717 Mhz
Hardware memory: 32652 megs

Free System resources: 90 %
GDI resources: 90 %
User resources: 90 %

Windows total applications running: 5
1 ,
2 , C:\Winapps\cargo\data\winweb.exe
*~*~*~*~*~*~*~*~*~*
Darrell Ortiz
CDM Software Solutions, Inc.
https://www.cdmsoft.com
User avatar
cdmmaui
 
Posts: 689
Joined: Fri Oct 28, 2005 9:53 am
Location: Houston ∙ Chicago ∙ Los Angeles ∙ Miami ∙ London ∙ Hong Kong

Re: JSON Post and Get sample

Postby Enrico Maria Giordano » Fri Jan 06, 2017 10:08 pm

You forgot Send() method.

EMG
User avatar
Enrico Maria Giordano
 
Posts: 8718
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia

Re: JSON Post and Get sample

Postby cdmmaui » Fri Jan 06, 2017 10:17 pm

Hello,

Thank you. I changed code and got it to run.

Response:
{"Type":"Request","Message":"Invalid username or password.","Code":"401","Params":[]}

I am checking with provider to confirm credentials are valid.

*********************************************************************************************
#include "FiveWin.ch"

REQUEST DBFCDX
REQUEST DBFFPT

//-----------------------------------------------------------------------------
function Main()

LOCAL oHttp, cHttpSend := ""

cHttpSend := [{ "Username": "xxx", "Password": "xxx"}]


oHttp:=CreateObject( "winhttp.winHttpRequest.5.1" )
oHttp:Open("POST","https://www.winwebconnect.com/api/v1/Login",.F.)
oHttp:SetRequestHeader( "Content-Type","application/json")
oHttp:SetRequestHeader( "Accept","application/json")
oHttp:Send( cHttpSend )
oHttp:waitForResponse()
MsgInfo( oHttp:ResponseText() )

RETURN 0
*~*~*~*~*~*~*~*~*~*
Darrell Ortiz
CDM Software Solutions, Inc.
https://www.cdmsoft.com
User avatar
cdmmaui
 
Posts: 689
Joined: Fri Oct 28, 2005 9:53 am
Location: Houston ∙ Chicago ∙ Los Angeles ∙ Miami ∙ London ∙ Hong Kong

Re: JSON Post and Get sample

Postby Enrico Maria Giordano » Fri Jan 06, 2017 11:09 pm

cdmmaui wrote: oHttp:waitForResponse()


This is not required.

EMG
User avatar
Enrico Maria Giordano
 
Posts: 8718
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia

Re: JSON Post and Get sample

Postby cdmmaui » Sat Jan 07, 2017 4:45 am

Hello,

Is there anything else I need to change to code below? I am not getting expected result.

******************** CODE ********************
#include "FiveWin.ch"

REQUEST DBFCDX
REQUEST DBFFPT

//-----------------------------------------------------------------------------
function Main()

LOCAL oHttp, cHttpSend := "", oClp, cResp

cHttpSend := [{ "Username": "xxx", "Password": "xxx"}]

oHttp:=CreateObject( "winhttp.winHttpRequest.5.1" )
oHttp:Open("POST","https://www.winwebconnect.com/api/v1/Login",.F.)
oHttp:SetRequestHeader( "Content-Type","application/json")
oHttp:SetRequestHeader( "Accept","application/json")

oHttp:Send( cHttpSend )

cResp := oHttp:ResponseText()

MsgInfo( cResp )

RETURN 0

******************** RESPONSE ********************
{"Type":"Request","Message":"Invalid username or password.","Code":"401","Params":[]}
*~*~*~*~*~*~*~*~*~*
Darrell Ortiz
CDM Software Solutions, Inc.
https://www.cdmsoft.com
User avatar
cdmmaui
 
Posts: 689
Joined: Fri Oct 28, 2005 9:53 am
Location: Houston ∙ Chicago ∙ Los Angeles ∙ Miami ∙ London ∙ Hong Kong

Re: JSON Post and Get sample

Postby anserkk » Sat Jan 07, 2017 5:10 am

Please try,

oHttp:SetRequestHeader( "Authorization",cHttpSend)

Please note that the format of cHttpSend depends on the format expected by your API provider. I assume that you have clear idea about it and you have filled the contents of cHttpSend accordingly

Regards

Anser
User avatar
anserkk
 
Posts: 1333
Joined: Fri Jun 13, 2008 11:04 am
Location: Kochi, India

Re: JSON Post and Get sample

Postby cdmmaui » Sat Jan 07, 2017 5:38 am

Dear Anser,

Thank you. I changed and got the following response.

{"Type":"api_error","Message":"The request failed due to an internal error.","Code":"500","Params":[{"Name":"WINConnect.Manager","Message":"Object reference not set to an instance of an object."},{"Name":"","Message":"One or more errors occurred."}]}

The API document indicate the following format. Should I change the code?

Login and Authentication:

Calling the Login with POST method endpoint obtains an initial authToken as a cookie and creates your secure
session in the WIN application. Each API request will return a fresh authToken cookie which must be passed
back to WIN in the next API request. Cookies are short text strings which the user agent (usually your web
browser but may be an HTTP library in .NET, Java or PHP, or a command line utility such as curl) stores and
manages. The only request that does not need an initial authToken cookie value is the Login. Your application
must also set headers for Content-type: application/json and Accept: application/json in the Login POST
request.

Example of Login POST request:
POST https://www.winwebconnect.com/api/v1/Login
{ "Username": "user@company.com", "Password": "secret"}
Headers:
Content-Type: application/json
Accept: application/json

Example of Login POST response:
Headers:
Cache-Control:no-cache
Content-Encoding:gzip
Content-Length:806
Content-Type:application/json; charset=utf-8
Date:Tue, 16 Dec 2014 07:00:05 GMT
Expires:-1
Pragma:no-cache
Set-Cookie:authToken=C9F3440891B1456002C223785F2C43581A00369CE7B6021E3B00FA21618FEBC51F0483
1AD0717A4FF7EF29A0882A38B9940E9E238CFE965ABA1C581399CCA2A804D663A521023F7953D40CC19EF6C
7CED0086B6F5F8D3C7A4352111247988B4D6789876D34A526823AF18E8DC4EE44BD; expires=Tue,
23-Dec-2014 07:00:05 GMT; path=/api; HttpOnly
Vary:Accept-Encoding
Each new Set-Cookie:authToken value returned from WIN must be provided in the next GET request header as
shown below (see GET UAT API Endpoint).
Upon successful login an object containing information about the logged in user will also be returned. Your
application does not need to store or do anything with this information.
{"LastLogin": "2014-12-16T10:58:52.293529Z", "UserDetails": { "ContactID": 674, "AgentID": 184145,
"FirstName": "UAT", "LastName": "APIUser", "Email": "UAT.apiuser@company.com", "JobTitle": "Software
Application", "ContactNumber": "+91889999999", "IsActivated": true, "OperatedBy": null, "Permissions":
["airtrackingapi" ]... }
*~*~*~*~*~*~*~*~*~*
Darrell Ortiz
CDM Software Solutions, Inc.
https://www.cdmsoft.com
User avatar
cdmmaui
 
Posts: 689
Joined: Fri Oct 28, 2005 9:53 am
Location: Houston ∙ Chicago ∙ Los Angeles ∙ Miami ∙ London ∙ Hong Kong

Re: JSON Post and Get sample

Postby anserkk » Sat Jan 07, 2017 9:40 am

With the below given code, I am getting the response from the server saying invalid username and password. Obviously the username and password is not the right one that I have used in the sample.
Please check with the correct username and password.

Code: Select all  Expand view  RUN
#Include "Fivewin.ch"
//--------------------------------
Function Main()

    Local oHttp
 
    Try
        oHttp := CreateObject( 'MSXML2.XMLHTTP' )
    Catch
        oHttp := CreateObject( 'Microsoft.XMLHTTP' )
    End
    oHttp:Open( 'POST', "https://www.winwebconnect.com/api/v1/Login", .F. )
   
    oHttp:SetRequestHeader( "Content-Type","application/json")
   
    oHttp:Send( '{ "Username": "user@company.com", "Password": "secret"}' )
    SysRefresh()
   
    MsgInfo(oHttp:ResponseBody)

Return

Regards
Anser
User avatar
anserkk
 
Posts: 1333
Joined: Fri Jun 13, 2008 11:04 am
Location: Kochi, India

Re: JSON Post and Get sample

Postby jnavas » Tue Nov 19, 2019 4:54 pm

Saludos y Gracias,

Code: Select all  Expand view  RUN

#Include "Fivewin.ch"
//--------------------------------
Function Main()

    Local oHttp
 
    Try
        oHttp := CreateObject( 'MSXML2.XMLHTTP' )
    Catch
        oHttp := CreateObject( 'Microsoft.XMLHTTP' )
    End

    oHttp:Open( 'POST', "https://petroapp-price.petro.gob.ve/price/", .F. )

    oHttp:SetRequestHeader( "Content-Type","application/json")
   
//    oHttp:Send( '{ "Username": "user@company.com", "Password": "secret"}' )
    SysRefresh()
   
    MsgInfo(oHttp:ResponseBody)

Return

 


Mensaje de Incidencia
Application
===========
Path and name: C:\FWH15\samples\PETROS.exe (32 bits)
Size: 2,938,880 bytes
Compiler version: Harbour 3.2.0dev (r1603082110)
FiveWin Version: FWH 15.03
Windows version: 6.2, Build 9200

Time from start: 0 hours 0 mins 0 secs
Error occurred at: 11/19/19, 12:53:18
Error description: (DOS Error -2147352567) WINOLE/1007 Unspecified error
(0x80004005): msxml3.dll

Stack Calls
===========
Called from: => TOLEAUTO:RESPONSEBODY( 0 )
Called from: PETROS.prg => MAIN( 22 )

System
======
CPU type: Intel(R) Core(TM) i7-4702MQ CPU @ 2.20GHz 2200 Mhz
Hardware memory: 8074 megs

Free System resources: 90 %
GDI resources: 90 %
User resources: 90 %

Windows total applications running: 4
1 ,
2 , C:\Windows\WinSxS\x86_microsoft.windows.common-controls_6595b64144ccf1df_5.82.9600.17810_none_7c5b6
3 GDI+ Window, C:\Windows\WinSxS\x86_microsoft.windows.gdiplus_6595b64144ccf1df_1.1.9600.19457_none_dadc49905bc8c0
4 Task Switching, C:\FWH15\samples\PETROS.exe

Variables in use
================
Procedure Type Value
==========================
TOLEAUTO:RESPONSEBODY
Param 1: O Class: ERROR
MAIN
Local 1: U
Local 2: U
User avatar
jnavas
 
Posts: 479
Joined: Wed Nov 16, 2005 12:03 pm
Location: Caracas - Venezuela

Re: JSON Post and Get sample

Postby anserkk » Wed Nov 20, 2019 4:04 am

Which version of Windows are you using? Windows 7 ?
Which version of Internet Explorer are you using ?
User avatar
anserkk
 
Posts: 1333
Joined: Fri Jun 13, 2008 11:04 am
Location: Kochi, India

Re: JSON Post and Get sample

Postby cnavarro » Wed Nov 20, 2019 11:40 am

The problem is that you have not sent anything, that is, you have not executed the Send ()

viewtopic.php?f=6&t=38068#p227461
Cristobal Navarro
Hay dos tipos de personas: las que te hacen perder el tiempo y las que te hacen perder la noción del tiempo
El secreto de la felicidad no está en hacer lo que te gusta, sino en que te guste lo que haces
User avatar
cnavarro
 
Posts: 6549
Joined: Wed Feb 15, 2012 8:25 pm
Location: España


Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: Otto and 84 guests