4DHTTPRequest Post Fields

If we need to POST our request to an API instead of using GET, where do we indicate the fields we are posting?

also–If the POST includes a file upload, the file is = the body, but what if we have additional datafields to post along with the file?

in php curl post without a file(just some datafields) to post, I set the $headers up as a simple array variable:

[“Content-Type: application/x-www-form-urlencoded”,”Host: login.microsoftonline.com”]

The $postfields were setup as a key=>value array variable.

Then do a php http_build_query function to the $postfields array and execute the php curl request with the followiing options:

		CURLOPT_HTTPHEADER => $headers,
		CURLOPT_POSTFIELDS => $postfields_string

just trying to see how to POST data and fields using the 4D.HTTPRequest

Thx!

what’s the api you are trying to use?

i’ve got a little library i wrote for managing get and post requests and communicate with quickbooks online, so there are a couple of references to “qbo” in the sample, below.

function _comms ( httpMethod : string , url : string , body : variant , overrideHeaders : variant ) -> responseO : object // body can be string or undefined. overrideHeaders can be object containing headers to be overridden of objects or undefined
	// httpMethod is GET, POST, PUT, PATCH, DELETE, HEAD, OPTIONS, etc.
	var headers, httpOptions: object
	var overrideHeaderKeys : collection
	var overrideHeaderKey , overrideHeaderValue : string
	var request : 4d.HTTPRequest
	var responseCode : integer
	var attempt : integer
	var retry : boolean
	var logMessage , logLevel : string
	var intuit_tid : string

	attempt = 1
	retry = true // first pass is the same as a retry

	repeat// may try 2x ( in case token/auth issue exists ) then fail
		retry = false // default to done
		headers = this._headers()
		//<handle override or supplemental headers>
			if ( ( not ( undefined ( overrideHeaders ) ) ) )
				overrideHeaderKeys = objectKeys ( overrideHeaders )
				forEach ( overrideHeaderKey , overrideHeaderKeys )
					overrideHeaderValue = overrideHeaders [ overrideHeaderKey ]
					headers [ overrideHeaderKey ] = overrideHeaderValue
				end //forEach ( overrideHeaderKey , overrideHeaderKeys )
			end //if ( ( not ( undefined ( overrideHeaders ) ) ) )
		//<handle override or supplemental headers>
		httpOptions = cs.httpRequestOptions.new ( httpMethod, headers , body )

		request = 4D.HTTPRequest .new ( url , httpOptions ) .wait()
		responseO = request.response
		responseCode = responseO.status
		logMessage = jsonStringify ( responseO )
		intuit_tid = responseO.headers.intuit_tid || ""

		switch
			: (responseCode==401)
				ds.qboAuth.refreshAccessToken()
				attempt += 1
				if ( attempt <= 2 ) // have tried 2x
					retry = true
				end // if ( attempt <= 2 )

				logMessage = logMessage + "\r\l\lWill retry."
				mlog ( "warn" , logMessage)
			: ( ( responseCode != 200 ) & ( intuit_tid != "" ) ) // error from quicken server
				mlog ( "warn" , logMessage )
			else // no error
				mlog ( "info" , logMessage )
		end //switch
	until ( not ( retry ) )
//---------------------------------------------------------------------------------------------------------------------------------------



function _headers() -> headers : object
	headers = ds.qboAuth.authHeaderO()
	headers [ "Accept" ] = "application/json"
	headers [ "Content-Type" ] = "application/json"
//---------------------------------------------------------------------------------------------------------------------------------------

I have some nice MS Graph functions I wrote in php and looking to adopt those. Initially I was just trying to use the HttpHandler feature of Qodly to upload a file to share folder and name it using a qodly custom file upload component.

I need to pass the file itself, and the filename using the POST method.

Just can’t see how to pass basic text key value params and the file using POST.

With GET you are normally passing the params in the querystring, but not for POST. Trying to see how to do that when post is multi-part—–the file itself, and some params.

see the code snipped i sent. you embed post data in the body.

do you need MS Graph functions to save the file somewhere else ?

Saving the file in the database and retrieving it regardless of if customer uploads a pdf or a jpg etc. was challenging. Using the HTTPHandler to process posted file was challenging. I have now been able to setup a shared singleton class called MSGraph which connects to our office 365 and o365 email via MS Graph. I have a function in the singleton class called MSGraph.me.createDocsFolder(custID:number). This will create the docsfolder to store a CUSTOMER’s docs in our graph and returns the graph folder id which is then saved to a field in the CUSTOMER record.

I now need a function to move a customer uploaded file into the new folder.

My current singelton function takes the url or path to the current file ON OUR SERVER…, gets the contents, then posts the file into the Graph customer folder.

I will need an uploader process that puts the file somewhere that my singleton function can get the contents of the file from a path or URL to it.

Is there any uploader or other approach that will just put the file on the server and get me the path to it so my singleton function can take that path as a parameter and use it to get the file contents and push the file to Graph in a rest call?

I think I could get it into the SHARED folder from previous comments, but you had concerns about getting it back out of SHARED to another server, so maybe I can try to see if the singleton function can get the contents of the file on SHARED, post to graph, then delete file from SHARED?

If i incorporate my company GRAPH into the APP, I can then also add functions to send emails from our domain when customer performs certain actions, which will be nice.

I have tried to make DropZone Work but am missing something.

Here is th page where the jpg or pdf is dropped:

Here is the Handler:

Here is the class/function Basically copied from the example:

Notice the yellow error lines. They say: The getHeader() etc. function is not part of the 4D.IncomingMessage Class. Wierd. I also notice that when type “request.” and the list of avl functions comes up, it is only a few of the many listed on 4D Documentation for IncomingMessage.

Would love to see what I am doing wrong…