Is it possible to attach file in POST Json? - json

I have a heap of data in format JSON(Serialized object).
I send this data to server by POST method with header: Content-Type: application/json.
Is it possible to attach file to body request and send at once. Or JSON data sugggests sending only text data?

In this context, the content-type header aims to describe the type of data in the request body.
If you use application/json the server will expect a JSON body.
If your goal is to send a single request with a JSON object and a file, you can either encode the file in the JSON structure (Probably base64. See: Binary Data in JSON String. Something better than Base64)
{
...
file: "encoded_content",
...
}
Or you can use the content type multipart/form-data.
A multipart is a part containing other part.
The first subpart may be the JSON strucuture. The second one may be the file

Try to send the file inside the json object as a base64 string:
{
"file":"dGhpcyBpcyBhIGZpbGUgc2FtcGxl..."
}
Later you can open the file with something like:
document.location = 'data:application/pdf;base64,' + file

Related

Can Insomnia convert an uploaded JSON to send a JSON body instead of a file?

I'm attempting to send a large JSON file to the system. The endpoint is expecting a JSON body, but if I copy paste the full JSON file into a JSON body, Insomnia crashes.
I thought of doing the "Multipart" option, but the file is transmitted as is, which isn't a JSON body.
Is there any way to tell Insomnia to take the contents of the JSON file and make a JSON body with it?

Pentaho HTTP Post multipart/form-data

I am trying to use HTTP Post to post the data by passing following Header and Body part,
Body:
One image URL having file path. Type as 'File'
Metadata in JSON format. Type as 'Text'
Headers:
Content-type = multipart/form-data
Authorization = Bearer 5412
Here is the screenshot of complete request,
This is how I have setup the HTTP Post task in Pentaho,
I found related post here using REST Client but this also didn't help: Pentaho HTTP Post using JSON
Really appreciate your help.
I think the problem is that "Post a file" doesn't mean include a file in the request, but rather to get the entire request from that file.
I don't know the specifics of how the data should look in the Post request, but the rough approach should be:
Pass the filename field to a Calculator step with the operation "Load file content to binary" to get a binary type field (ex: myimage)
Base64 (or other) encode the data with a Javascript step like this:
var encString = new Packages.java.lang.String( Packages.org.apache.commons.codec.binary.Base64.encodeBase64( myimage ) );
Wrap it in some content-type string indicating the encoding
Include the field in your request.
There is a patch on https://jira.pentaho.com/browse/PDI-14743 to enable proper binary transfer instead of String when using Rest Client.

Sample JSON request for multipart form

I want to call a rest API and my request body is composed in a json file. I want to send the request as multipart/form. Request body containst some text fields and several files for which I decide to use multipart form. I have found alot of logical example requests on internet but no concrete example in json format.
How would a sample json request body look like?
Finally found out that there is no such JSON representation of multipart/data requests. Infect thats just not possible using the json format. So multipart data request literally looks like the attached screenshot:

Converting SOAP Response XML to JSON using XSJS

I'm having problems to convert an XML response file to JSON using XSJS, Any help will be appreciated. Thanks!
It is actually pretty simple. While sending the request using XSJS to the server...your URL path should contain the format specifier.
http://myhost:8000/exampleapp/resource.xsjs/some/more/options?$format=JSON
or if you do not want to append to the URL then include the following line your request header
accept: application/json

How does server handle JSON in the body of a POST

I am using jQuery to post JSON data to a tomcat server and the server is handling the JSON array data perfectly! It is as though I passed key=value request parameters along with the URL.
So why am I posting this? I would like to know how the server treats JSON in the body of a request and how the data ends up being interpreted as request parameters. I have Googled my a** off and all I find is how the server sends JSON back to the client.
$.ajax() converts JSON data into key-value pairs (querystring style) by default. You need to set { processData : false } in the AJAX request to keep it as raw JSON.