Forum OpenACS Development: Re: http POST X binary file

Posted by Gustaf Neumann on

If you want to submit a POST request with a certain content-type to transmit a file "/tmp/foo.jpg", use:

set req_headers [ns_set create]
ns_set put $req_headers "Content-Type" "image/jpeg"
ns_http run -method POST -headers $req_headers -body_file /tmp/foo.jpg https://localhost:8443/upload

One can use as well the OpenACS API, where files can passed explicitly: util::http::post. The interface supports submission of multiple files. Probably, the data is sent via multipart/form-data, you have to double-check.

Posted by Iuri Sampaio on
Hi Gustaf,
I'm sorry, I believe I haven't expressed myself well in the previous post.

ns_http comes with the header "Content-type" "application/x-www-form-urlencoded" by default.

Thus, when I add the extra header "Content-Type" "image/jpeg", it gets doubled, instead of overwriting the default one

The reason for that is "ad_form context". That POST request happens in the middle of another POST request, while ad_form -on_submit event has been executed.

"ns_http run" snippet is placed still within the block -on_submit, the image is sent to a third/external site, right before the image gets stored in the ACS content repository.

Unfortunately, writing ns_http post request in a different file, avoiding context overlapping, then redirecting to the workflow/execution of the form, is not a solution. Ad_form has been used in the GUI, so that everything starts with the ad_form submission.

The external site functions as some sort of validation to the image being uploaded. Meaning, if the endpoint returns negatively
the users will be required to chose/use a valid image.

So, how would I remove "Content-type" "application/x-www-form-urlencoded"?

Do you see any other approach that could allow running this post request out of the ad_form context?

NS release is NaviServer/4.99.19 (tar-4.99.19) running

[17/Jan/2021:13:38:25][10351.7efbf3d70700][-conn:qonteo:default:0:2287-] Notice: Running REST debug upload
[17/Jan/2021:13:38:25][10351.7efbf3d70700][-conn:qonteo:default:0:2287-] Notice: HEADER
: t0
[17/Jan/2021:13:38:25][10351.7efbf3d70700][-conn:qonteo:default:0:2287-] Notice: HEADERS 8
[17/Jan/2021:13:38:25][10351.7efbf3d70700][-conn:qonteo:default:0:2287-] Notice: Host X-Real-IP Connection close Content-Length 16925 Content-type application/x-www-form-urlencoded Content-Type image/jpeg User-Agent NaviServer/4.99.19
[17/Jan/2021:13:38:25][10351.7efbf3d70700][-conn:qonteo:default:0:2287-] Notice: BODY
: \xc3\xbf\xc3\x98\xc3\xbf\xc3\xa0\xc0\x80\x10JFIF\xc0\x80\x01\x01\x01\xc0\x80H\xc0\x80H\xc0\x80\xc0\x80\xc3\xbf\xc3\x9b\xc0\x80C\xc0\x80\x03\x02\x02\x03\x02\x02\x03\x03\x03\x03\x04\x03\x03\x04\x05\x08\x05\x05\x04\x04\x05
: \x07\x07\x06\x08\x0c
: \x0c\x0c\x0b
: \x0b\x0b\x0d\x0e\x12\x10\x0d\x0e\x11\x0e\x0b\x0b\x10\x16\x10\x11\x13\x14\x15\x15\x15\x0c\x0f\x17\x18\x16\x14\x18\x12\x14\x15\x14\xc3\xbf\xc3\x9b\xc0\x80C\x01\x03\x04\x04\x05\x04\x05\x09\x05\x05\x09\x14\x0d\x0b\x0d\x14\x14\x14\x14\x14\x14\x14\x14\x14\x14\x14\x14\x14\x14\x14\x14\x14\x14\x14\x14\x14\x14\x14\x14\x14\x14\x14\x14\x14\x14\x14\x14\x14\x14\x14\x14\x14\x14\x14\x14\x14\x14\x14\x14\x14\x14\x14\x14\x14\x14\xc3\xbf\xc3\x82\xc0\x80\x11\x08\xc0\x80\xc3\xba\xc0\x80\xc3\xba\x03\x01\x11\xc0\x80\x02\x11\x01\x03\x11\x01\xc3\xbf\xc