Why we use json and xml for web api rather then othen platform like jquery and array.
Its interview based question and I was enable to response .
JSON and XML are ways to format and data-interchange.
JQuery is a library was built on top of javascript. so it has nothing to do in data-interchange.
Array by its definition: its a method for storing information. in other words you can use arrays in any format you want to store data.
In Conclusion, Web API is a service that provides or gathers data. and in general you can exchange data between client and server by JSON, XML, or you can use whatever data format you want such as HTML. JQuery can be use to call the Web API that can return an array of data in any data format.
This is not compulsory that you have to return the result in JSON or XML, but most of the time we are returning values in these format because there are media type formatter for JSON and XML, the capability of media type formatter is to read the CLR object from HTTP message and to write the CLR object into HTTP message. This is the main reason that we return values in JSON and XML format.
Related
I want to create a Student Register and Login Api without using serializer in django Rest Framework.
So I want to know how I make CRUD operation for those api using ApiView
Any one please solve this
I don't think you can, or even should but first, you need to understand what a serializer actually does:
Serializers allow complex data such as querysets and model instances to be converted to native Python datatypes that can then be easily rendered into JSON, XML, or other content types. Serializers also provide deserialization, allowing parsed data to be converted back into complex types, after first validating the incoming data
So don't be scared by serializers. Just take your time and learn how to use them using the many tutorials available online.
If you are using DRF then you must need to create the serializers. But you can create the API without using DRF and serialzers.
Serializer is not just for converting json data ,it also used to validate payload. You can use request.data.get("field_name_in_json_object") and fill the fields one by one.and for return you can use .values("field_name") in
querysets.
Django values_list vs values
I am writing an API which accept files, upload it in s3 & dump the url in DynamoDB's table. As the payload would contain the files so it needs to be a multipart form data.
When I generally accept JSON as payload I use Pydantics to validate payload schema, attribute's datatype and its values. I am not aware of how to validate the multipart form data in using Pydantic. Is it possible to validate multipart form data using Pydantics? If yes any short example would be helpful.
I am able to send image/file and normal key value which is being served as normal json later. I am using the form-data type of body in postman and a node server.
To handle image i am using multer on my node server.
But what makes issue is when i try to send the nested json and image together.
I am not able to do this thing.
Everything is fine but this is how the nested json is logging in the terminal :-
Please! Any help would be great to get the nested data object also in actual json format but not like this string as shown in terminal photo.
JSON can't contain binary data. What you're asking isn't directly possible.
The ideal thing to do is a multipart request, which is what you're getting in your first example. Note that one of those parts could be JSON, and you can just reference the other part by name, or with some other identifier.
The wrong way to do this is to base64 encode the data and put it in your JSON. If you do this, you'll get what you're asking for at the expense of 33% overhead in file size, wasted CPU and memory on each end for encoding/decoding, and significant waste in memory for your JSON parser which now has to chew through all of this extra data.
An alternative is to use a format that supports binary data, like CBOR. CBOR works in browsers, is streamable, supports all of the types of JSON and then some, is extensible, and standardized.
One solution is to split image upload and record upload into two separate services and call in UI one after the other.
It may be late but I faced the same issue, what I did was,
1: send data through form-data body and had 2 parameters.
file (the file which we want to send on backend using file field)
data (a string in json format using the text field in form-data).
example:
data : {
"words":500,
"model":0,
"video":true,
"user_id":"user1"
}
I sent this in the request. Remember that when we send using form-data we have 2 separate choices file/text. Since this is text so the whole string is considered a text. So, essentially the data parameter is a string. When I receive this request on backend in django I do the following to get the actual json in json format.
attributes = json.loads(request.data['data'])
This converts the data parameter which was first a string, into a json just as we wanted.
Hope it helps others.
I am working on an Apache Camel project. Basically the Jetty endpoint takes a Http Post request and the message goes through a few steps of transformation in the route. The last step of transformation is through JAXB, which converts Java object into XML. The Java DSL is below
final DataFormat jaxb = new JaxbDataFormat("sample");
from("jetty:http://localhost:8888/foo")
.unmarshal(format).split(body()).marshal(jaxb)
My problem is that when I send a POST request to the localhost URL, the HTTP response is string
[sample.Claims#b68e0e], not the XML I expected. This is the JAXB object ID. When I changed the DSL to
from("jetty:http://localhost:8888/foo")
.unmarshal(format).split(body()).marshal(jaxb).to("stream:out")
I can see the XML print out correctly in the stdout. I don't know how to make the HTTP Response to contain XML instead of the Object ID. Any help is appreciated.
Update:
I want to clarify what I try to accomplish. I need to convert a delimited string to an xml document. The post message to Jetty endpoint is a delimited string. The route first uses BeanIO to convert the string into a POJO and the POJO into XML using JAXB. Even though the post message is a single line string, I have to use split() because BeanIO by default dealing with multi-line flat file. I followed #Peter's suggestion by adding aggegate after the jaxb marshaling as below
from("jetty:http://localhost:8888/transformProxy/ECSProxy")
.unmarshal(format).split(body()).marshal(jaxb)
.aggregate(constant("1"),new MyAggregationStrategy())
.completionSize(1).to("stream:out");
but it does not seem to make any difference. I still get [sample.Claims#1a631c2] as the Http response body, while the stdout prints out the correct xml document. I am not sure how/when the response body of the jetty endpoint is set.
I made it work by moving the aggregate strategy as the second parameters of the split. For details, please see https://camel.apache.org/splitter.html. Search for the "Split aggregate request/reply sample"
I'm interested can I store into client side for example large hashmap or a List? I need something like a temporary cache to store user session data.
In theory, you could use HTML5 client side storage in JavaScript. So far now, no JSF components exists which could do the job transparently. You'd need to write all the necessary JS code yourself or grab jQuery.
All JSF as being a HTML code generator can do for you is to print Java objects in JSON format as a JavaScript variable assignment with help of a JSON library such as Google Gson.
<h:outputScript>var data = #{bean.dataAsJSON};</h:outputScript>
The getDataAsJSON() should just return an already converted JSON string.