Topics   All   Mac OS X (Only)   Windows (Only)   Linux (Only, Not)   iOS (Only, Not)  
Components   Crossplatform Mac & Win   Server (Not)   Client   Old   Guides   Examples
New in version: 6.1   6.2   6.3   6.4   6.5   7.0   7.1   7.2   7.3   7.4    Statistic  

CURL.SetOptionPost

Sets transfer to be a HTTP Post.

Component Version macOS Windows Server FileMaker Cloud FileMaker iOS SDK
CURL 2.5 Yes Yes Yes Yes Yes

MBS( "CURL.SetOptionPost"; Handle; Value )

Parameters

Parameter Description Example value
Handle The CURL session handle. $curl
Value Whether this is a POST request. 1

Result

Returns "OK" on success.

Description

Sets transfer to be a HTTP Post.
A parameter set to 1 tells the library to do a regular HTTP post. This will also make the library use a "Content-Type: application/x-www-form-urlencoded" header. (This is by far the most commonly used POST method).

Use one of CURL.SetOptionPostFields options to specify what data to post and CURL.SetOptionPostFieldSize or CURL.SetOptionPostFieldSizeLarge to set the data size.

Optionally, you can provide data to POST using the CURL.SetInput* functions but then you must make sure to not set CURL.SetOptionPostFields to anything but "". When providing data with a callback, you must transmit it using chunked transfer-encoding or you must set the size of the data with the SetOptionPostFieldSize or CURL.SetOptionPostFieldSizeLarge option. To enable chunked encoding, you simply pass in the appropriate Transfer-Encoding header.

You can override the default POST Content-Type: header by setting your own with CURL.SetOptionHTTPHeader.

Using POST with HTTP 1.1 implies the use of a "Expect: 100-continue" header. You can disable this header with CURL.SetOptionHTTPHeader as usual.

If you use POST to a HTTP 1.1 server, you can send data without knowing the size before starting the POST if you use chunked encoding. You enable this by adding a header like "Transfer-Encoding: chunked" with CURL.SetOptionHTTPHeader. With HTTP 1.0 or without chunked transfer, you must specify the size in the request.

When setting CURL.SetOptionPost to 1, it will automatically set CURL.SetOptionNoBody to 0 (since 7.14.1).

If you issue a POST request and then want to make a HEAD or GET using the same re-used handle, you must explicitly set the new request type using CURL.SetOptionNoBody or CURL.SetOptionGet or similar.

For debugging, the website https://www.posttestserver.com is very useful. It allows you to see what is received on the other end for a post operation.

Examples

Enables POST option:

MBS( "CURL.SetOptionPost"; $curl; 1 )

Using Rosette web service:

# Start new CURL transfer
Set Variable [$curl; Value:MBS("CURL.New")]
# Set options like Verbose Debug Messages
Set Variable [$result; Value:MBS("CURL.SetOptionVerbose"; $curl; 1)]
# the url of webservice
Set Variable [$result; Value:MBS("CURL.SetOptionURL"; $curl; "https://api.rosette.com/rest/v1/morphology/parts-of-speech")]
# the header info for tagging, API Key comes from a global field.
Set Variable [$result; Value:MBS("CURL.SetOptionHTTPHeader"; $curl; "user_key: " & RosetteAPISettings::RosetteAPIKey; "Content-Type: application/json"; "Accept: application/json" )]
# We want a POST transfer:
Set Variable [$result; Value:MBS("CURL.SetOptionPost"; $curl; 1 )]
# Build JSON using the plugin function to properly encode a string:
Set Variable [$request; Value:"{\"content\": " & MBS("JSON.CreateString"; TextData::TextAsInput) & "}"]
Set Variable [$result; Value:MBS("CURL.SetOptionPostFields"; $curl; $request)]
#show our JSON in a field for debugging:
Set Field [TextData::CURLInput; $request]
#Run transfer
Set Variable [$result; Value:MBS("CURL.Perform"; $curl)]
# Check result and debug messages
Set Field [TextData::CurlDebug; MBS("CURL.GetDebugAsText"; $curl)]
Set Field [TextData::CurlOutput; MBS("CURL.GetResultAsText"; $curl)]
# Cleanup
Set Variable [$r; Value:MBS("CURL.Cleanup"; $curl)]

Charge with Stripe webservice:

# new session
Set Variable [$curl; Value:MBS("CURL.New")]
# use stripe charge API
Set Variable [$result; Value:MBS("CURL.SetOptionURL"; $curl; "https://api.stripe.com/v1/charges")]
# set user name
Set Variable [$result; Value:MBS("CURL.SetOptionUsername"; $curl; "sk_test_ your id here")]
# make a post with given content:
Set Variable [$result; Value:MBS("CURL.SetOptionPost"; $curl; 1)]
Set Variable [$result; Value:MBS("CURL.SetOptionPostFields"; $curl; "amount=400&currency=usd&description=Charge%20for%20test@example.com&source[object]=card&source[number]=4242424242424242&source[exp_month]=12&source[exp_year]=2017&source[cvc]=123")]
# perform
Set Field [CURL Test::Result; MBS("CURL.Perform"; $curl)]
# now check result
Set Field [CURL Test::Text; MBS("CURL.GetResultAsText"; $curl; "UTF8")]
Set Field [CURL Test::header; MBS("CURL.GetDebugAsText"; $curl)]
# cleanup
Set Variable [$result; Value:MBS("CURL.Cleanup"; $curl)]

See also

Example Databases


CURL.SetOptionPort   -   CURL.SetOptionPostFieldSize

Feedback: Report problem or ask question.




Links
MBS Xojo Plugins