How to generate "Content-Type: application/octet-stream" with Zinc client

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

How to generate "Content-Type: application/octet-stream" with Zinc client

chicoary
This post was updated on .
I thank in advance if anyone can help me.

The code below:

        ZnClient new
  url: 'http://127.0.0.1:5001/add?stream-channels=true';
                addPart: (ZnMimePart fieldName: 'file' fileNamed: 'file.txt');
  post.

generates the following response using netcat:

nc -l 5001
POST /add?stream-channels=true HTTP/1.1
User-Agent: Zinc HTTP Components 1.0
Content-Length: 194
Host: 127.0.0.1:5001
Content-Type: multipart/form-data;boundary=Boundary-Zn-IDMNDZTQ
Accept: */*

--Boundary-Zn-IDMNDZTQ
Content-Disposition: form-data;name="file";filename="file.txt"
Content-Length: 16
Content-Type: text/plain;charset=utf-8

file.txt content
--Boundary-Zn-IDMNDZTQ--

however I would like to get:

nc -l 5001
POST /add?stream-channels=true HTTP/1.1
User-Agent: Zinc HTTP Components 1.0
Content-Length: 194
Host: 127.0.0.1:5001
Content-Type: multipart/form-data;boundary=Boundary-Zn-IDMNDZTQ
Accept: */*

--Boundary-Zn-IDMNDZTQ
Content-Type: application/octet-stream
Content-Disposition: form-data;name="file";filename="file.txt"
Content-Length: 16

file.txt content
--Boundary-Zn-IDMNDZTQ--

I not figured out how to get the extra line "Content-Type: application / octet-stream" using Zinc.
Reply | Threaded
Open this post in threaded view
|

Re: How to generate "Content-Type: application/octet-stream" with Zinc client

Sven Van Caekenberghe-2
Hi,

That is an odd request.

Can you produce it with curl ?

You could try to make your part something like this:

| part |
part := ZnMimePart fieldName: 'file' value: '123'.
part headers at: 'Content-Type' add: ZnMimeType imageJpeg.
part.

This creates two values for Content-Type.

Sven

> On 10 Feb 2016, at 00:25, chicoary <[hidden email]> wrote:
>
> I thank in advance if anyone can help me.
>
> The code below:
>
>        ZnClient new
>  url: 'http://127.0.0.1:5001/add?stream-channels=true';
>                addPart: (ZnMimePart fieldName: 'file' fileNamed:
> 'file.txt');
>  post.
>
> generates the following response using netcat:
>
> nc -l 5001
> POST /add?stream-channels=true HTTP/1.1
> User-Agent: Zinc HTTP Components 1.0
> Content-Length: 194
> Host: 127.0.0.1:5001
> Content-Type: multipart/form-data;boundary=Boundary-Zn-IDMNDZTQ
> Accept: */*
>
> --Boundary-Zn-IDMNDZTQ
> Content-Disposition: form-data;name="file";filename="file.txt"
> Content-Length: 16
> Content-Type: text/plain;charset=utf-8
>
> file.txt content
> --Boundary-Zn-IDMNDZTQ--
>
> however I would like to get:
>
> nc -l 5001
> POST /add?stream-channels=true HTTP/1.1
> User-Agent: Zinc HTTP Components 1.0
> Content-Length: 194
> Host: 127.0.0.1:5001
> Content-Type: multipart/form-data;boundary=Boundary-Zn-IDMNDZTQ
> Accept: */*
>
> --Boundary-Zn-IDMNDZTQ
> Content-Type: application/octet-stream
> Content-Disposition: form-data;name="file";filename="file.txt"
> Content-Length: 16
> Content-Type: text/plain;charset=utf-8
>
> file.txt content
> --Boundary-Zn-IDMNDZTQ--
>
> I not figured out how to get the extra line "Content-Type: application /
> octet-stream" using Zinc.
>
>
>
> -----
> http://chicoary@...
> --
> View this message in context: http://forum.world.st/How-to-generate-Content-Type-application-octet-stream-with-Zinc-client-tp4876671.html
> Sent from the Pharo Smalltalk Users mailing list archive at Nabble.com.
>


Reply | Threaded
Open this post in threaded view
|

Re: How to generate "Content-Type: application/octet-stream" with Zinc client

chicoary
Thanks Sven for your reply.

The curl I want reproduce with Zinc is:

curl 'http://localhost:5001/api/v0/add?stream-channels=true' \
-H 'content-type: multipart/form-data; boundary=a831rwxi1a3gzaorw1w2z49dlsor' \
-H 'Connection: keep-alive' \
--data-binary $'--a831rwxi1a3gzaorw1w2z49dlsor\r\nContent-Type: application/octet-stream\r\nContent-Disposition: file; name="file"; filename="file.txt"\r\n\r\nfile.txt contents--a831rwxi1a3gzaorw1w2z49dlsor--' --compressed

It is like an example in https://github.com/ipfs/js-ipfs-api#tooling.

I want to implement the ipfs add command in my project http://smalltalkhub.com/#!/~chicoary/SmallIPFS/.

Reply | Threaded
Open this post in threaded view
|

Re: How to generate "Content-Type: application/octet-stream" with Zinc client

kuszi
Hello!

Here is a code snippet:


At the trivial pülace you can set whatever mime type yout think will work for you.
I hope this helps.

Robert
--------------------------------------------------------------


sendFile

|contents stream |

stream :=  FileStream readOnlyFileNamed: self file.
stream binary.
contents := stream contents.
stream close.

 self requestContext respond: [ :response |
       response
           contentType: 'application/x-msdownload';
           document: contents;
           attachmentWithFileName: self fileName
].




chicoary <[hidden email]> ezt írta (időpont: 2016. febr. 10., Sze, 14:08):
Thanks Sven for your reply.

The curl I want reproduce with Zinc is:

curl 'http://localhost:5001/api/v0/add?stream-channels=true' \
-H 'content-type: multipart/form-data;
boundary=a831rwxi1a3gzaorw1w2z49dlsor' \
-H 'Connection: keep-alive' \
--data-binary $'--a831rwxi1a3gzaorw1w2z49dlsor\r\nContent-Type:
application/octet-stream\r\nContent-Disposition: file; name="file";
filename="file.txt"\r\n\r\nfile.txt
contents--a831rwxi1a3gzaorw1w2z49dlsor--' --compressed

It is like an example in https://github.com/ipfs/js-ipfs-api#tooling.

I want to implement the ipfs add command in my project
http://smalltalkhub.com/#!/~chicoary/SmallIPFS/.





-----
http://chicoary@...
--
View this message in context: http://forum.world.st/How-to-generate-Content-Type-application-octet-stream-with-Zinc-client-tp4876671p4876782.html
Sent from the Pharo Smalltalk Users mailing list archive at Nabble.com.

Reply | Threaded
Open this post in threaded view
|

Re: How to generate "Content-Type: application/octet-stream" with Zinc client

kuszi
OK. I'm an idiot. This is server code, so, another business....


Robert Kuszinger <[hidden email]> ezt írta (időpont: 2016. febr. 10., Sze, 14:15):
Hello!

Here is a code snippet:


At the trivial pülace you can set whatever mime type yout think will work for you.
I hope this helps.

Robert
--------------------------------------------------------------


sendFile

|contents stream |

stream :=  FileStream readOnlyFileNamed: self file.
stream binary.
contents := stream contents.
stream close.

 self requestContext respond: [ :response |
       response
           contentType: 'application/x-msdownload';
           document: contents;
           attachmentWithFileName: self fileName
].




chicoary <[hidden email]> ezt írta (időpont: 2016. febr. 10., Sze, 14:08):
Thanks Sven for your reply.

The curl I want reproduce with Zinc is:

curl 'http://localhost:5001/api/v0/add?stream-channels=true' \
-H 'content-type: multipart/form-data;
boundary=a831rwxi1a3gzaorw1w2z49dlsor' \
-H 'Connection: keep-alive' \
--data-binary $'--a831rwxi1a3gzaorw1w2z49dlsor\r\nContent-Type:
application/octet-stream\r\nContent-Disposition: file; name="file";
filename="file.txt"\r\n\r\nfile.txt
contents--a831rwxi1a3gzaorw1w2z49dlsor--' --compressed

It is like an example in https://github.com/ipfs/js-ipfs-api#tooling.

I want to implement the ipfs add command in my project
http://smalltalkhub.com/#!/~chicoary/SmallIPFS/.





-----
http://chicoary@...
--
View this message in context: http://forum.world.st/How-to-generate-Content-Type-application-octet-stream-with-Zinc-client-tp4876671p4876782.html
Sent from the Pharo Smalltalk Users mailing list archive at Nabble.com.

Reply | Threaded
Open this post in threaded view
|

Re: How to generate "Content-Type: application/octet-stream" with Zinc client

Sven Van Caekenberghe-2
In reply to this post by chicoary
From your netcat output I inferred that you wanted two content-type headers, but I don't think you do, I guess you just want to control the content-type.

When creating a ZnPart based on a file, the content-type is inferred automatically from the filename's extension.

The simplest solution is to use an unknown file type, like .bin or .dat which will result in a content-type of application/octet-stream (the default).

ZnMimePart fieldName: 'file' fileNamed: '/tmp/foo.bin'.

You could try setting the content-type after creation.

| part |
part := ZnMimePart fieldName: 'file' fileNamed: '/tmp/foo.txt'.
part headers contentType: ZnMimeType default.
part.

The methods #setContentType: could be added to ZnMimePart, I guess.

> On 10 Feb 2016, at 13:42, chicoary <[hidden email]> wrote:
>
> Thanks Sven for your reply.
>
> The curl I want reproduce with Zinc is:
>
> curl 'http://localhost:5001/api/v0/add?stream-channels=true' \
> -H 'content-type: multipart/form-data;
> boundary=a831rwxi1a3gzaorw1w2z49dlsor' \
> -H 'Connection: keep-alive' \
> --data-binary $'--a831rwxi1a3gzaorw1w2z49dlsor\r\nContent-Type:
> application/octet-stream\r\nContent-Disposition: file; name="file";
> filename="file.txt"\r\n\r\nfile.txt
> contents--a831rwxi1a3gzaorw1w2z49dlsor--' --compressed
>
> It is like an example in https://github.com/ipfs/js-ipfs-api#tooling.
>
> I want to implement the ipfs add command in my project
> http://smalltalkhub.com/#!/~chicoary/SmallIPFS/.
>
>
>
>
>
> -----
> http://chicoary@...
> --
> View this message in context: http://forum.world.st/How-to-generate-Content-Type-application-octet-stream-with-Zinc-client-tp4876671p4876782.html
> Sent from the Pharo Smalltalk Users mailing list archive at Nabble.com.
>


Reply | Threaded
Open this post in threaded view
|

Re: How to generate "Content-Type: application/octet-stream" with Zinc client

chicoary
This post was updated on .
Sven, with the code you suggested inserted in context of my project SmallIPFS

add: aFilePath
        ^ZnClient new
  url: prefix , '/add?stream-channels=true';
                addPart: ((ZnMimePart fieldName: 'file' fileNamed: aFilePath) headers contentType: ZnMimeType default);
  post.

and invoking

ipfs add: 'file.txt'

I get

nc -l 5001
POST /api/v0/add?stream-channels=true HTTP/1.1
User-Agent: Zinc HTTP Components 1.0
Content-Length: 176
Host: 127.0.0.1:5001
Content-Type: multipart/form-data;boundary=Boundary-Zn-BCJQCIUT
Accept: */*

--Boundary-Zn-BCJQCIUT
Content-Disposition: form-data;name="file";filename="file.txt"
Content-Length: 16
Content-Type: application/octet-stream

--Boundary-Zn-BCJQCIUT--


Very good!

Thanks a lot.