It is out of desperation and I don't know java. Basically, I want to resolve a couchbase lite Document back to a react-native application as follows:
Document doc = database.getDocumet('id');
promise.resolve(doc);
that doesn't work with error can't be cast so I turned to toString(), hoping to parse it in react-native:
promise.resolve(doc.toString());
// in js:
const document = getNativeDocument('id');
console.log(document)
const v = JSON.parse(document);
but that fails too with error:
SyntaxError: JSON Parse error: Unexpected identifier "Document"].
The console result is:
Document{#0xb3181c18e30a7f1-1f61-49a0-9ca0-1dc3d2e343ed#1-a7ed608736c34cf5860a0d35968e6441e1f089ef(..):obj=>Dictionary{(..)a=>A,b=>B},age=>20,firstPromo=>null}
I have tried solutions to convert the Document into other times that can easily be resolved by promise.resolve() but now wondering if there is away to work with results of the .toString()
the toString method generates an internal representation of the object for debugging, not JSON of the document. You need to use a different method to serialize your Document to JSON.
The most popular option for this is to use the Jackson library: https://www.baeldung.com/jackson-object-mapper-tutorial
If you're using Spring framework with Spring data couchbase connector then Jackson's ObjectMapper should already be available to your application as a Bean.
Thanks for helping!
I'm trying to get a ID from a Response Data, but i tryied everything and didn't work, let me show:
I want that ORGKEY as a Variable
I run POST, after they give me a ORGKEY
And i need the ORGKEY in POST TOKEN
POST TOKEN needs a orgKey
And the orgkey are a random numbers and letters! I tryed everything but didn't work, so i try here.
Thank you for your pacient ;)
Add JSON Extractor as a child of the request which returns this JSON response
Configure it as follows:
Names of created variables: anything meaningful, i.e. ORGKEY
JSON Path Expressions: $..orgKey
Match No.: 1
That's it, you should be able to refer the extracted value as ${ORGKEY} where required
More information:
JsonPath Examples
API Testing With JMeter and the JSON Extractor
Your JSON response
{
"result":{
"returnCodel":"success",
"orgKey":"3b6car5lkckvgx52wf",
"message":"Organizaçào criada com sucesso!"
}
}
You can use JSON Extractor or JSON JMSEPath Extractor to extract the orgKey from the response.
I have a situation where I need to store my SOAP response in a string in case of success.
soap(soapActionBuilder -> soapActionBuilder.client("xyzclient").receive().messageType(MessageType.XML).validate("xapth validation", "Success"));
The above code is working if we receive a success response, but now I need to store that SOAP response in a string and return it.
I am not sure how I can do that. if anyone is having any idea please share, I am new to citrus. Thanks in advance.
If you would like to capture some value from response body, you could use a method extractFromPayload. This methods takes two parameters:
path - path to XML element
variable - to store element into variable for further usage in citrus test.
Sample how to use it:
soap().client("client")
.receive()
.extractFromPayload("//Foo/bar","foobar");
Now you can use variable foobar like this ${foobar}
echo("Extracted var: ${foobar}")
when using the below code using XML it works perfectly, that is, the navigation links are returned successfully. However, when I change the format to "json" or "application/json", the links2 navigation links list is empty, meaning that rental.getNavigations() returns an empty list.
Can anyone help please? I am using Apache Olingo for Java OData v4.
Thanks
URI uri = client.newURIBuilder(serviceRoot)
.appendEntitySetSegment("Rentals")
.appendKeySegment(1).format("application/xml").build();
ODataRetrieveResponse<ODataEntity> response2 = client.getRetrieveRequestFactory().getEntityRequest(uri).execute();
ODataEntity rental = response2.getBody();
List<ODataLink> links2 = rental.getNavigationLinks();
for (ODataLink link : links2) {
System.out.println(link.getRel());
System.out.println(link.getName());
URI linkUri = client.newURIBuilder(serviceRoot)
.appendNavigationSegment(link.getLink().toString()).format("atom").build();
ODataRetrieveResponse<ODataEntity> responseCustomer
= client.getRetrieveRequestFactory().getEntityRequest(linkUri).execute();
ODataEntity cust = responseCustomer.getBody();
if(link.getName().equals("Stock"))
System.out.println(cust.getProperty("Status").getValue().toString());
else System.out.println(cust.getProperty("Name").getValue().toString());
}
The odata.metadata=full format parameter is necessary to get odata.navigationLink properties to appear in the JSON response. Add odata.metadata=full to the format option when building the client. The full format should be application/json;odata.metadata=full. If you have access to the request headers via the client object, you might consider setting the Accept header instead.
Background
We are building a Restful API that should return data objects as JSON. In most of the cases it fine just to return the data object, but in some cases, f.ex. pagination or validation, we need to add some metadata to the response.
What we have so far
We have wrapped all json responses like this example:
{
"metadata" :{
"status": 200|500,
"msg": "Some message here",
"next": "http://api.domain.com/users/10/20"
...
},
"data" :{
"id": 1001,
"name": "Bob"
}
}
Pros
We can add helpful metadata to the response
Cons
In most cases we don't need the metadata field, and it adds complexity to the json format
Since it's not a data object any more, but more like a enveloped response, we can not use the response right away in f.ex backbone.js without extracting the data object.
Question
What is the best practices to add metadata to a json response?
UPDATE
What I've got so far from answers below:
Remove the metadata.status an return the http response code in the
http protocol instead (200, 500 ...)
Add error msg to body of an http 500 repsonse
For pagination i natural to have some metadata telling about the pagination structure, and the data nested in that structure
Small amount of meta data can be added to http header (X-something)
You have several means to pass metadata in a RESTful API:
Http Status Code
Headers
Response Body
For the metadata.status, use the Http Status Code, that's what's for!
If metadata is refers to the whole response you could add it as header fields.
If metadata refers only to part of the response, you will have to embed the metadata as part of the object.DON'T wrap the whole response in an artifical envelope and split the wrapper in data and metadata.
And finally, be consistent across your API with the choices you make.
A good example is a GET on a whole collection with pagination. GET /items
You could return the collection size, and current page in custom headers. And pagination links in standard Link Header:
Link: <https://api.mydomain.com/v1/items?limit=25&offset=25>; rel=next
The problem with this approach is when you need to add metadata referencing specific elements in the response. In that case just embed it in the object itself. And to have a consistent approach...add always all metadata to response. So coming back to the GET /items, imagine that each item has created and updated metadata:
{
items:[
{
"id":"w67e87898dnkwu4752igd",
"message" : "some content",
"_created": "2014-02-14T10:07:39.574Z",
"_updated": "2014-02-14T10:07:39.574Z"
},
......
{
"id":"asjdfiu3748hiuqdh",
"message" : "some other content",
"_created": "2014-02-14T10:07:39.574Z",
"_updated": "2014-02-14T10:07:39.574Z"
}
],
"_total" :133,
"_links" :[
{
"next" :{
href : "https://api.mydomain.com/v1/items?limit=25&offset=25"
}
]
}
Note that a collection response is an special case. If you add metadata to a collection, the collection can no longer be returned as an array, it must be an object with an array in it. Why an object? because you want to add some metadata attributes.
Compare with the metadata in the individual items. Nothing close to wrapping the entity. You just add some attributes to the resource.
One convention is to differentiate control or metadata fields. You could prefix those fields with an underscore.
Along the lines of #Charlie's comment: for the pagination part of your question you still need to bake the metadata into the response somhow, but the status and message attributes here are somewhat redundant, since they are already covered by the HTTP protocol itself (status 200 - model found, 404 - model not found, 403 - insufficient privs, you get the idea) (see spec). Even if your server returns an error condition you can still send the message part as the response body. These two fields will cover quite much of your metadata needs.
Personally, I have tended towards (ab)using custom HTTP headers for smaller pieces of metadata (with an X- prefix), but I guess the limit where that gets unpractical is pretty low.
I've expanded a bit about this in a question with a smaller scope, but I think the points are still valid for this question.
I suggest you to read this page https://www.odata.org/ You are not forced to use OData but the way they do the work is a good example of good practice with REST.
We had the same use case, in which we needed to add pagination metadata to a JSON response. We ended up creating a collection type in Backbone that could handle this data, and a lightweight wrapper on the Rails side. This example just adds the meta data to the collection object for reference by the view.
So we created a Backbone Collection class something like this
// Example response:
// { num_pages: 4, limit_value: 25, current_page: 1, total_count: 97
// records: [{...}, {...}] }
PageableCollection = Backbone.Collection.extend({
parse: function(resp, xhr) {
this.numPages = resp.num_pages;
this.limitValue = resp.limit_value;
this.currentPage = resp.current_page;
this.totalCount = resp.total_count;
return resp.records;
}
});
And then we created this simple class on the Rails side, to emit the meta data when paginated with Kaminari
class PageableCollection
def initialize (collection)
#collection = collection
end
def as_json(opts = {})
{
:num_pages => #collection.num_pages
:limit_value => #collection.limit_value
:current_page => #collection.current_page,
:total_count => #collection.total_count
:records => #collection.to_a.as_json(opts)
}
end
end
You use it in a controller like this
class ThingsController < ApplicationController
def index
#things = Thing.all.page params[:page]
render :json => PageableCollection.new(#things)
end
end
Enjoy. Hope you find it useful.
How about returning directly the object that you want in data, like return:
{
"id": 1001,
"name": "Bob"
}
And return in headers the metadata.
Option 1 (one header for all metadata JSON):
X-METADATA = '{"status": 200|500,"msg": "Some message here","next": "http://api.domain.com/users/10/20"...}'
Option 2 (one header per each metadata field):
X-METADATA-STATUS = 200|500
X-METADATA-MSG = "Some message here",
X-METADATA-NEXT = "http://api.domain.com/users/10/20"
...
Until now I was using like you, a complex JSON with two fields, one for data and one for metadata. But I'm thinking in starting using this way that I suggested, I think it will be more easy.
Remind that some server have size limit for HTTP headers, like this example: https://www.tutorialspoint.com/What-is-the-maximum-size-of-HTTP-header-values
JSON:API solves this by defining top-level meta and data properties.