Validating your CRBasic code for HTTPPost()
and HTTPPut()
What’s the difference between HTTPPost
and HTTPPut
?
The HTTP POST method sends data in the request body to a web server, typically for storage. It is commonly used for file uploads and web form submissions. POST helps keep data private and allows the transmission of large amounts of data when needed.
In essence, HTTP Post says, "Here’s the data."
The HTTP PUT method requests the enclosed entity be stored at the supplied URI/URL. If the URI/URL refers to an already existing resource, it modifies it, and if the URI/URL does not point to an existing resource, then the server can create the resource or object with that URI/URL. Unlike HTTPPost(), PUT can create new resources. The data sent represents the complete entity itself. PUT allows multiple resource creations and eliminates the need to check for duplicate submissions.
In essence, HTTP Put says, "Here’s the resource. Create it, or I’m updating the resource you already have."
The parameters for the HTTPPost()
and HTTPPut()
instructions in CRBasic are identical. This document uses HTTPPost()
in its examples.
HTTPResult=HTTPPost
("https://posttestserver.dev/p/4p73smwyp8rev73x/post"
, ContentsString,HTTPResponse,HTTPHeader,0,0,Min,8,7500)
HTTPResult=HTTPPut
("https://posttestserver.dev/p/4p73smwvyp8rev73x/post"
, ContentsString,HTTPResponse,HTTPHeader,0,0,Min,8,7500)
-
Instruction placement – There are reasons you might want to put the
HTTPPost()
orHTTPPut()
instruction in the mainScan
loop, but it is more common to put them in aSlowSequence
scan after the mainScan
:NOTE:You can also put these instructions in a subroutine.
Example:
SlowSequence
Scan
(1,Hr,3,0)HTTPResult=
HTTPPost
("https://posttestserver.dev/p/4p73smwvyp8rev73x/post"
, ContentsString,HTTPResponse,HTTPHeader,0,0,Min,8,7500)NextScan
EndProg
-
Ensure the
HTTPPost()
orHTTPPut()
has aResult
variable you can monitor to verify the transaction was executed successfully. Declare theHTTPResult
value at the top of your program in yourPublic
variables:Public
HTTPResultAs Long
Then include it on the front of the
HTTPPost()
instruction, followed by an equal sign:
HTTPResult=HTTPPost
("https://posttestserver.dev/p/4p73smwvyp8rev73x/post"
,"Test"
,HTTPResponse,HTTPHeader,0,0,Min,8,7500)
The response codes are listed in this document under step 2 in Verify result and response codes.
-
Set or confirm the address in the URI/URL is correct:
HTTPResult=HTTPPost
("https://posttestserver.dev/p/4p73smwyp8rev73x/post"
,"Test"
,HTTPResponse,HTTPHeader,0,0,Min,8,7500)
-
Define the Contents you will send to the HTTP server. You can specify a table name in quotes to send the data from that table, or you can enter a string variable (not in quotes) to include the information you want to Post or Put to the server. You can also send files from the CPU:, CRD:, USB:, or USR: drive by preceding the file name with the drive.
HTTPResult=
HTTPPost
("https://posttestserver.dev/p/4p73smwvyp8rev73x/post"
,"Test"
,HTTPResponse,HTTPHeader,0,0,Min,8,7500)HTTPResult=
HTTPPost
("https://posttestserver.dev/p/4p73smwvyp8rev73x/post"
, ContentsString,HTTPResponse,HTTPHeader,0,0,Min,8,7500)HTTPResult=
HTTPPost
("https://posttestserver.dev/p/4p73smwvyp8rev73x/post"
,"CPU:Batt_Volt.csv"
,HTTPResponse,HTTPHeader,+ CHR(13)+ CHR(10) + http_header_content) -
Set or Confirm a variable has been declared at the top of your program and is used as the
HTTPResponse
parameter. This variable needs to be set as aType String
underPublic
variables and should be large enough to store a full success response or error message from the HTTP server you are querying:Public
ResponseAs String
* 200
HTTPResult=HTTPPost
("https://posttestserver.dev/p/4p73smwvyp8rev73x/post"
,"Test"
,HTTPResponse,HTTPHeader,0,0,Min,8,7500)
A quick reference for the most common Response codes is contained in this document in Verify result and response codes. For a more complete reference go to Quick reference for HTTPPost(), HTTPPut(), and HTTPGet() errors.
-
Set or Confirm the
HTTPHeader
parameter. If your request doesn’t require a header, use empty quotes for this parameter. For long headers, declare a string variable in thePublic
variables section of your data logger program, ensuring it is large enough to hold the entire header. The example below uses a string variable namedHTTPHeader
as the header parameter:Public
HTTPHeaderAs String
* 2500
HTTPResult=HTTPPost
("https://posttestserver.dev/p/4p73smwvyp8rev73x/post"
,"Test"
,HTTPResponse,HTTPHeader,0,0,Min,8,7500)
NOTE:It is generally best to set the header in your CRBasic program just before sending the HTTP request. This allows useful information from the server’s response to be stored in the header, which can then be parsed in your data logger program for further use.
-
If you are streaming data from a data table, you need to specify the
NumRecsStream
,IntervalStream
, andUnitsStream
parameters. If you are sending a file (e.g.,USR:myfile.dat
), then you do not need to specify those parameters.NOTE:The
NumRecsStream
,IntervalStream
, andUnitsStream
are only used when streaming data directly from a data table or data table field.
HTTPResult=HTTPPost
("https://posttestserver.dev/p/4p73smwvyp8rev73x/post"
,"Test"
,HTTPResponse,HTTPHeader,0,0,Min,8,7500)
HTTPResult=HTTPPost
("https://posttestserver.dev/p/4p73smwvyp8rev73x/post"
,"Test"
,HTTPResponse,HTTPHeader,0,0,Min,8,7500)
If you POST or PUT data from a table based on the number of unsent records, ensure you specify the number of records in the
NumRecsStream
field and set theIntervalStream
value to 0.If you POST or PUT data from a table based on a time interval, the
NumRecsStream
parameter functions as aTimeIntoInterval
parameter and must be specified. For example, settingNumRecsStream
to 0,IntervalStream
to 60, andUnitsStream
to Min means theHTTPPost()
orHTTPPut()
instruction will execute at the start of each 60-minute interval.Using a
NumRecsStream
of 0 and aIntervalStream
of 0 will tell the data logger to send all previously unsent data when theHTTPPost()
orHTTPPut()
is called.Ensure you specify the time units for the
NumRecsStream
andIntervalStream
parameters in theUnitsStream
variable. Sec, Min, Hr, and other options are available.
HTTPResult=HTTPPost
("https://posttestserver.dev/p/4p73smwvyp8rev73x/post"
,"Test"
,HTTPResponse,HTTPHeader,0,0,Min,8,7500)
-
Specify the
FileOption
parameter. This parameter is only needed if you are streaming a file. It lets you choose the file format of yourHTTPPost()
orHTTPPut()
request. A number of options are available including Binary, ASCII, XML, and JSON.NOTE:Option 8 is the standard Campbell Scientific data file format created by LoggerNet.
HTTPResult=HTTPPost
("https://posttestserver.dev/p/4p73smwvyp8rev73x/post"
,"Test"
,HTTPResponse,HTTPHeader,0,0,Min,8,7500)
-
Check the
Timeout
parameter. This parameter is optional and measured in 0.01-second intervals. If not specified, the default value of 7500 (75 seconds) is used.The
Timeout
parameter determines how long the data logger will wait for a response before marking the connection attempt as a failure and incrementing the result parameter from Step 1.If the
Timeout
is too short, theHTTPGet()
instruction may not receive a response before the data logger stops listening, especially when using HTTPS or TLS, which require additional processing time. The default value of 7500 is generally sufficient for mostHTTPGet()
transactions.
HTTPResult=HTTPPost
("https://posttestserver.dev/p/4p73smwvyp8rev73x/post"
,"Test"
,HTTPResponse,HTTPHeader,0,0,Min,8,7500)