Hi,
The case is, im using postman/swagger to send the request, and i want to return the response with a file or attachment (which is i need to send using multipart is it?).
Is it possible to use YAJL or HTTPAPI?
i've tried to use yajl_writeStdout() from YAJL, when i read the YAJLR4 on the yajl_writeStdout(), i can only change the HTTP Headers is it?
and i've tried to use EXAMPLE7 as well from HTTPAPI, on that example, the http_url_post_stmf will send the post request instead of return the response is it?
CMIIW and sorry im a newly
Thanks before,
RianSuhito
Is it possible to send back JSON responses with a file or attachment?
-
- Posts: 2
- Joined: Fri Jun 30, 2023 9:39 am
-
- Site Admin
- Posts: 782
- Joined: Sun Jul 04, 2021 5:12 am
Re: Is it possible to send back JSON responses with a file or attachment?
This doesn't sound related to HTTPAPI. HTTPAPI is for consuming HTTP URLs, not for providing them.
YAJL can be used to do the JSON portion of it. YAJL is a JSON tool -- it specializes in JSON, it doesn't know anything about other formats such as MIME or whatever format your attachment is expected to be in.
You'll need to write a multipart encoder -- this isn't too difficult.
The output you'll need to write will look something like this:
The boundary can be anything that doesn't appear in the actual content -- I tend to like to put the timestamp in there figuring that the odds of it being in the content are rare, plus even if it does happen to exist, if you retry, it'll be different the next time.
The boundary needs to be the same everywhere, though... except that each "part" of the multipart document (or "attachment" if you choose to think of it that way) must start with an extra "--". And the very end of the document must have "--BOUNDARY-HERE--" so must both start and end with "--".
You will need to know the correct Content-Type for whatever you're sending... you were vague on what else besides JSON is involved here.
Once you've built the data, you can send it with the QtmhWrStout routine. (You can't use yajl_writeStdout, as this will only since the JSON data that YAJL has generated, and then will be done.)
YAJL can be used to do the JSON portion of it. YAJL is a JSON tool -- it specializes in JSON, it doesn't know anything about other formats such as MIME or whatever format your attachment is expected to be in.
You'll need to write a multipart encoder -- this isn't too difficult.
The output you'll need to write will look something like this:
Code: Select all
Status: 200
Content-Type: multipart/form-data; boundary=BOUNDARY--2023-07-03-12:13:14.000000
--BOUNDARY--2023-07-03-12:13:14.000000
Content-Disposition: form-data; name="myjsondata"; filename="my_file.json"
Content-Type: application/json
{ "json": "data here" }
--BOUNDARY--2023-07-03-12:13:14.00000
Content-Disposition: form-data; name="other.file.here"; filename="other_file.dat"
Content-Type: application/octet-stream
whatever the data in the attachment should be
--BOUNDARY--2023-07-03-12:13:14.00000--
The boundary needs to be the same everywhere, though... except that each "part" of the multipart document (or "attachment" if you choose to think of it that way) must start with an extra "--". And the very end of the document must have "--BOUNDARY-HERE--" so must both start and end with "--".
You will need to know the correct Content-Type for whatever you're sending... you were vague on what else besides JSON is involved here.
Once you've built the data, you can send it with the QtmhWrStout routine. (You can't use yajl_writeStdout, as this will only since the JSON data that YAJL has generated, and then will be done.)
-
- Posts: 2
- Joined: Fri Jun 30, 2023 9:39 am
Re: Is it possible to send back JSON responses with a file or attachment?
Hi Scott,
Thanks for your reply!
So if your sample code is executed, the expected result is 2 file which is my_file.json and other_file.dat ?
The data of my_file.json which is { "json": "data here" } , its expected to be inside the my_file.json that the requester can download?
I've tried to build the simple boundary, but i got something that can't be read.. is it because of ccsid?
I've got something like this on the response:
``�����������
Im sorry if this content is out of topic from YAJL or HTTPAPI.
Thanks for your reply!
So if your sample code is executed, the expected result is 2 file which is my_file.json and other_file.dat ?
The data of my_file.json which is { "json": "data here" } , its expected to be inside the my_file.json that the requester can download?
I've tried to build the simple boundary, but i got something that can't be read.. is it because of ccsid?
I've got something like this on the response:
``�����������
Im sorry if this content is out of topic from YAJL or HTTPAPI.
-
- Site Admin
- Posts: 782
- Joined: Sun Jul 04, 2021 5:12 am
Re: Is it possible to send back JSON responses with a file or attachment?
I would assume that it's due to you writing the data in the wrong character encoding (aka CCSID) but I can't say for sure, as I haven't debugged your program.