Project

General

Profile

Actions

Requete POST and base64

How to send datas using base64 encoding by a POST request ?

  • Base64-encoded data takes about 33% more space than the original data.
  • This encoding is designed to make binary data survive transport through transport layers that are not 8-bit clean, such as mail bodies.

You should know how to send a POST request : Request_POST before continue

In Scol, to encode to base64, use _base64_encode_ function.

base64_encode : fun [S] S

The first step is the encoding. The second step should be the conversion from any character to web character : _strtoweb_

strtoweb : fun [S] S

This example sends an image to a server (via a PHP script).

fun CBreceive (inet, u, data, err)=
  if err == 0 then
  (
    _fooS strcat ">>>>>>>CBreceive current>>>>>>> " data;
    let u -> [s] in
    mutate u <- [strcat s data];
    0
  )
  else if err == 1 then
  (
    let u -> [s] in
    _fooS strcat ">>>>>>>CBreceive end>>>>>>> " s;
    _closemachine
  )
  else
  (
    _fooS strcat ">>>>>>>CBreceive err>>>>>>> " itoa err;
    0
  );;

fun main ()=
  _showconsole;
  let _getpack _checkpack "examples/image.jpg" -> file in  // get the file content
  let base64_encode file -> file64 in  // encode to base64
  let strtoweb file64 -> file64web in  // convert to web character
  let "http://domin.tld/post.php" -> url in
  let strcat "image=" file64web -> data in
  INETGetURLex2 _channel "POST" url "content-type: application/x-www-form-urlencoded" data 0 @CBreceive [""];
  0;;
  • With the SYSPACK API, you can use base64encodePweb :
fun CBreceive (inet, u, data, err)=
  // the same thing than above
  0;;

fun main ()=
  _showconsole;
  let _getpack _checkpack "examples/image.jpg" -> file in  // get the file content
  let base64encodePweb file -> file64web in  // encode to base64 and convert it
  let "http://domin.tld/post.php" -> url in
  let strcat "image=" file64web -> data in
  INETGetURLex2 _channel "POST" url "content-type: application/x-www-form-urlencoded" data 0 @CBreceive [""];
  0;;

base64encodePweb seems slightly faster than base64_encode and strtoweb. base64encodePweb is based from the PHP source code.

Author : iri
Date : december 2011

Return to Examples

Updated by iri about 12 years ago ยท 2 revisions