wwHTTP::AddPostKey

Set POST data to send on a POST or PUT request.

This method sets the post buffer and supports encoding for HTML form based uploads as well as raw data buffers.

Several different POST mechanisms are available:

  • UrlEncoded Form Data nHttpPostMode=1 (default)
  • Multi-Part Forms nHttpPostMode=2
  • Raw Data cContentType="application/json"

The two HTTP Form post modes - UrlEncoded (1) and Multi-Part (2) are supported via the nHttpPostMode flag which triggers specific content encoding of the key value pairs passed.

For raw Data content like JSON, XML, or binary data like images, pdfs, zip etc. you pass the data in the tcKey parameter, and set the cContentType property with the data's content type.

For HTTP File Upload form keys using multi-part forms, please use the .AddPostFile() method (new in v7).

Note: For raw data requests you can also pass the POST data directly to the .Post() or Put() methods as parameters, instead of using .AddPostKey() explicitly (new in v7).

o.AddPostKey(tcKey, tcValue)

Return Value

nothing

Parameters

tcKey
The key of the value to set or if tcValue is not passed, the entire raw POST buffer to send.

If no parameters are passed the entire PostBuffer is cleared.

tcValue
The value to set the key to. In combination these values are equivalent to a Form Variable name and value.

Multi-Part Forms specific parameters

Remarks

UrlEncoded content that is longer than 254 characters requires wwIPStuff.dll for the URLEncoding using C++ code for better performance. This refers to the default nHttpPostMode value of 1.

Depending on the value of the nHTTPPostMode property forms are posted either in standard URLEncoded or Multi-Part form mode. Multi-part form mode can be more efficient for large uploads as no encoding takes place, but the server application must support parsing multi-part forms. Web Connection automatically handles this via Request.Form().

If you are posting data larger than 16mb which is FoxPro's string size limit, you have use the loHttp.lUseLargePostBuffer = .T. to force using custom temporary cursor based storage for post data, so that data can be fed in one large chunk that can bypass the 16mb string limit. For more info see this blog post.

Example

loHTTP = CREATEOBJECT("wwHTTP")

*** Standard HTML style UrlEncoded Form Variables
loHTTP.AddPostKey("Company","West Wind")
loHTTP.AddPostKey("Name","Rick Strahl")

lcHTML = loHTTP.Post("http://www.west-wind.com/wconnect/testpage.wwd")

*** Posting JSON
loHTTP.AddPostKey()  && Clear POST buffer
lcJson = FILETOSTR("someJsonData.json")

*** Raw post buffer
loHTTP.cContentType = "application/json"
loHTTP.AddPostKey(lcJson)   && Just the raw data, no key

lcJson = loHTTP.Post("http://www.west-wind.com/wconnect/testJson.wwd")

*** Posting a multi-part form with a File Upload
loHTTP.AddPostKey()  && Clear existing POST buffer

loHTTP.nHttpPostMode = 2  && Multipart form encoding

*** Post a file and a regular form variable
loHttp.AddPostFile("File","d:\temp\wws_invoice.pdf",.T.)
loHttp.AddPostKey("txtFileNotes","test note")

lcHTML = loHTTP.Post("http://localhost/wconnect/FileUpload.wwd")

* Note: Prior to v7 you can use .HttpGet() instead of .Post() or .Put()

See also:

Class wwHttp | wwHTTP::nhttppostmode |

© West Wind Technologies, 1996-2024 • Updated: 02/13/24
Comment or report problem with topic