JSON API

Source code related resources

Moderator: Rathinagiri

Post Reply
majkll_ns
Posts: 16
Joined: Sun Oct 24, 2010 12:50 pm

JSON API

Post by majkll_ns »

I need a help.

For this request

curl -X POST "https://esite/api/publicApi" -H "accept: text/plain" -H "ApiKey: xxxxx" -H "Content-Type: application/json" -d "{\"registrationNumber\":\"yyyy\"}"

this work fine:

oRestApi := Win_OleCreateObject( "MSXML2.ServerXMLHTTP" )
oRestApi:setTimeouts(nTimeout * 1000 /* nResolve */ , nTimeout * 1000 /* nConnect*/ , nTimeout * 1000 /* nSend */, nTimeout * 1000 /* nReceive
*/ )

oRestApi:Open( "POST", cUrl, .F. )
oRestApi:setRequestHeader( "accept", "text/plain")
oRestApi:setRequestHeader( "ApiKey", "xxxxxx" )
oRestApi:setRequestHeader( "Content-Type", "application/json")

cBody1 := { => }
cBody1 ["registrationNumber"] := yyyyy
cBody := hb_jsonEncode( cBody1 , .F. )

oRestApi:Send(cBody)
cReturn := hb_jsonDecode( oRestApi:ResponseText() )


Question is how to code this:


curl -X POST "https://esite/api/publicApi" -H "accept: text/plain" -H "ApiKey: xxxx" -H "Content-Type: multipart/form-data" -F "ublFile=@test.xml;type=text/xml"
edk
Posts: 914
Joined: Thu Oct 16, 2014 11:35 am
Location: Poland

Re: JSON API

Post by edk »

majkll_ns wrote: Sat Nov 27, 2021 10:29 am curl -X POST "https://esite/api/publicApi" -H "accept: text/plain" -H "ApiKey: xxxx" -H "Content-Type: multipart/form-data" -F "ublFile=@test.xml;type=text/xml"
Maybe try in this way:

Code: Select all

Function UploadFileApi( cUrl, cFile )

Local cFileUpload       := hb_FNameNameExt ( cFile )
Local cHexDateTimeStamp := hb_NumToHex (  Val ( hb_TtoS( hb_DateTime() ) + StrZero( hb_RandomInt( 0, 99 ), 2 ) ) ) 
Local cBoundary         := PadLeft ( cHexDateTimeStamp, 40, '-' )
Local cBoundarySep      := '--' + cBoundary + CRLF
Local cFormBody         := ''
Local cContentType

cFormBody += cBoundarySep

cFormBody += 'Content-Disposition: form-data; name="ublFile"; filename="' + cFileUpload + '"' + CRLF

SWITCH Upper(hb_FNameExt ( cFileUpload ) )
	CASE ".XML"
		cContentType := 'text/xml'
		EXIT
	CASE ".JPG"
		cContentType := 'image/jpeg'
		EXIT
	CASE ".JPEG"
		cContentType := 'image/jpeg'
		EXIT
	CASE ".BMP"
		cContentType := 'image/bmp'
		EXIT
	CASE ".PNG"
		cContentType := 'image/png'
		EXIT
	OTHERWISE
		cContentType := 'image/jpeg'
END SWITCH
	
cFormBody += 'Content-Type: ' + cContentType + CRLF + CRLF

cFormBody += FileStr( cFile )

cFormBody += CRLF

cFormBody += '--' + cBoundary + '--' + CRLF		//EndOfBoundary

oRestApi:Open( "POST", cUrl, .F. )
oRestApi:setRequestHeader( "accept", "text/plain")
oRestApi:setRequestHeader( "ApiKey", "xxxxxx" )
oRestApi:setRequestHeader( "Content-Length", AllTrim ( Str ( Len ( cFormBody ) ) ) )
oRestApi:setRequestHeader( "Content-Type", "multipart/form-data; boundary=" + cBoundary )		
	
oRestApi:Send( cFormBody )
majkll_ns
Posts: 16
Joined: Sun Oct 24, 2010 12:50 pm

Re: JSON API

Post by majkll_ns »

Thank you very much!
This works and it is just what I need.
Post Reply