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."

NOTE:

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)
 

  1. Instruction placement – There are reasons you might want to put the HTTPPost() or HTTPPut() instruction in the main Scan loop, but it is more common to put them in a SlowSequence scan after the main Scan:

    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
     

  2. Ensure the HTTPPost() or HTTPPut() has a Result variable you can monitor to verify the transaction was executed successfully. Declare the HTTPResult value at the top of your program in your Public variables:

     
    Public HTTPResult As 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.

  3. 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)
     

  4. 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)

  5. 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 a Type String under Public variables and should be large enough to store a full success response or error message from the HTTP server you are querying:

     
    Public Response As 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.

  6. 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 the Public variables section of your data logger program, ensuring it is large enough to hold the entire header. The example below uses a string variable named HTTPHeader as the header parameter:

     
    Public HTTPHeader As 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.

  7. If you are streaming data from a data table, you need to specify the NumRecsStream, IntervalStream, and UnitsStream 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, and UnitsStream 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 the IntervalStream value to 0.

    If you POST or PUT data from a table based on a time interval, the NumRecsStream parameter functions as a TimeIntoInterval parameter and must be specified. For example, setting NumRecsStream to 0, IntervalStream to 60, and UnitsStream to Min means the HTTPPost() or HTTPPut() instruction will execute at the start of each 60-minute interval.

    Using a NumRecsStream of 0 and a IntervalStream of 0 will tell the data logger to send all previously unsent data when the HTTPPost() or HTTPPut() is called.

    Ensure you specify the time units for the NumRecsStream and IntervalStream parameters in the UnitsStream 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)
     

  8. Specify the FileOption parameter. This parameter is only needed if you are streaming a file. It lets you choose the file format of your HTTPPost() or HTTPPut() 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)
     

  9. 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, the HTTPGet() 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 most HTTPGet() transactions.

     
    HTTPResult=HTTPPost ("https://posttestserver.dev/p/4p73smwvyp8rev73x/post", "Test",HTTPResponse,HTTPHeader,0,0,Min,8,7500)