Re: Can I http POST a file

new topic     » goto parent     » topic index » view thread      » older message » newer message
ChrisB said...

Hi

Exactly the same way as I want it I would imagine.

I'm still waiting my test login from the drug company, but curl looks like the way forward so far - the command line curl being the simpler to use ( curl -d "filename=@filename" url ), then I'll look at euLibCurl once thats running.

Chris

According to curl's man page

-d/--data <data> 
              (HTTP)  Sends the specified data in a POST request to the HTTP 
              server, in the same way that a browser does when  a  user  has 
              filled  in  an  HTML  form and presses the submit button. This 
              will cause curl to pass the data to the server using the  con‐ 
              tent-type   application/x-www-form-urlencoded.    Compare   to 
              -F/--form. 
 
              -d/--data is the same as --data-ascii.  To  post  data  purely 
              binary,  you  should  instead use the --data-binary option. To 
              URL-encode the value of a form field you may use --data-urlen‐ 
              code. 
 
              If  any  of  these  options is used more than once on the same 
              command  line,  the  data  pieces  specified  will  be  merged 
              together   with   a   separating  &-symbol.  Thus,  using  '-d 
              name=daniel -d skill=lousy' would generate a post  chunk  that 
              looks like 'name=daniel&skill=lousy'. 
 
              If  you start the data with the letter @, the rest should be a 
              file name to read the data from, or - if you want curl to read 
              the data from stdin.  The contents of the file must already be 
              URL-encoded. Multiple files can  also  be  specified.  Posting 
              data from a file named 'foobar' would thus be done with --data 
              @foobar. 

So, untested and taken from Eu4's docs, the following should work:

include std/net/http.e 
include std/io.e 
 
sequence inputfile = "YOURFILE" 
sequence data 
sequence file_content 
file_content = read_file(inputfile, BINARY_MODE) 
-- According to net.e source, if sequence(data[1]) then contet type is urlencoded 
data = { { "filename", file_content, inputfile, "text/plain", ENCODE_BASE64 } } -- You may try ENCODE_NONE here 
object result = http_post("http://example.com", data)  
if atom(result) then  
   printf(1, "Web error: %d\n", result)  
    abort(1)  
end if  

Regards,
Guillermo

Edit: I've just realized that -d "filename=@filename" doesn't include the file contents, I thought it did, I went ahead and tested without filename= part and that includes the content but it seems to concatenate lines without EOL separators which I guess is not the desired input.

To achieve a similar POST request (this is correctly splitted with EOL chars in Eu4 simply change

data = { { "filename", file_content, inputfile, "text/plain", ENCODE_BASE64 } } -- You may try ENCODE_NONE here 
data = file_content 

Edit2: Should've been BINARY_MODE

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu