Search code examples
httppostapldyalog

How do I POST raw data in Dyalog APL HttpCommand?


I am trying to submit data as plain text using HttpCommand object. I know how to do it with cURL, but I'm unsure how to specify the data in my HttpCommand object. Here's how I would do it with cURL:

curl -request POST "http://my-api-url" --header "Content-Type: text/plain" --data-raw "here is my test"

What's the equivalent way to send the plain text data in my HttpCommand object?


Solution

  • The most direct analogous HttpCommand equivalent is:

          r←HttpCommand.Do 'post' 'http://my-api-url' 'here is my test' ('content-type' 'text/plain')
    

    The basic syntax for HttpCommand.Do is:

          r←HttpCommand.Do 'Command' 'URL' Params Headers 
    
    • Command is the HTTP method (GET, POST, PUT, etc)

    • URL is the URL

    • Params the data for the request. If the Command is 'GET', HttpCommand will URLEncode Params and include it as the query string of the request. Otherwise, it will be send as the payload (body) of the request.

    • Headers is a set of 0 or more name/value pairs to be used as the HTTP headers for the request. It can be any of:

      • a 2-column matrix - (2 2⍴'content-type' 'text/plain' 'accept' 'text/html')
      • a vector of 2-element vectors of strings - (('content-type' 'text/plain')('accept' 'text/html'))
      • a vector with an even number of strings - ('content-type' 'text/plain' 'accept' 'text/html')

    r will be a namespace containing information about the response.
    r.rc is the return code where 0 indicates your request was successfully sent and a response receieved.
    r.HttpStatus is the HTTP status code. A 2XX status means that the request was successful.
    r.Data is the response payload (body), if any was sent by the server.
    r.Headers is a 2-column matrix of the response's HTTP header name/value pairs.
    r contains other elements as well.

    There are other ways to accomplish the same task using HttpCommand that may be useful - for instance if you want to issue multiple requests to the same host.

    These are all covered in the HttpCommand documentation