Convert object to Json-formatted string in Jscript .Net - fiddler

In Fiddler, I have a response body (application/json) that I convert into an Object using eval() (if there is a better way please let me know) to perform some checks. Now I want to convert several parts (but not all parts) of the object back into a string and save them to seperate files. How would I do this in Jscript?
I have tried using the ToString() method but it only returns [object Object]. Any ideas?

I had the same question and I asked Fiddler Forum about it.
From the answer, I cooked this FiddlerScript (JScript) to answer yours:
var sInput = '{"a" : 1, "b" : [2, 3, 4]}';
var oJSON = Fiddler.WebFormats.JSON.JsonDecode(sInput);
FiddlerApplication.Log.LogFormat('input: {0}', sInput);
FiddlerApplication.Log.LogFormat('oJSON: {0}', oJSON.ToString());
FiddlerApplication.Log.LogFormat('oJSON["a"]: {0} (expected: 1)', oJSON.JSONObject["a"]);
FiddlerApplication.Log.LogFormat('oJSON["b"] : {0} (expected: 2)', oJSON.JSONObject["b"][0]);
I hope this will help after half a year...

JScript.NET does not itself include a JSON serializer.
You can either use one from the appropriate .NET assembly, or you can use the JSON serializer baked into Fiddler; have a look at the Fiddler.WebFormats.JSON.JsonEncode(object) function.

Related

How do I insert an ISODate into mongoDB via Postman?

How do I insert an ISODate into MongoDB via Postman? I have looked around but examples/queries on this subject tend to be just for ways of getting various string formats.
I have an in-house API set up on my localhost so I am querying the database (MongoDB) with Postman. Queries & entries are written in JSON so I would do this like so usually:
{ "adminModifiedId": 1, "dateCreated" : { "$date": "1557510188"}, .., .. }
or
{ "adminModifiedId": 1, "dateCreated" : new Date(), .., .. }
Of course dates within MongoDB are in this format: ISODate("2019-01-21T17:41:27.107Z") but I just can't find the right solution here. I know that Postman does allow to set global & environmental variables within the Pre-request Script section but it does seem strange that a platform so established would not have a way to format or convert into an ISODate type.
Edited in response to #Danny_Dainton
Postman body as JSON request
Pre-request Script
Erorr response
I'll leave this for a few days to see if anyone can suggest a pre-established answer (that doesn't require a pre-request script). Otherwise I will mark mine correct as the only answer that has worked for me so far.
You could use either of these methods in a Pre-request Script.
Using the moment lib, like this:
var moment = require('moment')
pm.globals.set("ISO_Date", moment())
More info about that here: https://stackoverflow.com/a/47823708/6028443
Or
Just use basic JS code like this to create the same timestamp:
pm.globals.set("ISO_Date", (new Date()).toISOString())
Once the variable is created, add {{ISO_Date}} reference to your request body.
For whatever reason other solutions didn't work for me but may for others. This one resolved my issue though so may be of use.
Pre-request Script
let t = Date.now()
pm.environment.set('t', t);
Body (sample)
{ "adminModifiedId": 1, "dateCreated" : { "$date": {{t}}}, .., .. }

Alamofire formatting POST/PUT parameters swift

I have an issue using Alamofire on iOS (swift) :
When I try to send a request (in .POST or .PUT) with parameters like that :
parameters:["description": WhoGives, "images": (
{
container = offerImages;
name = "563f993e4b00ddad7ed42790_0.jpg";
},
{
container = offerImages;
name = "563f993e4b00ddad7ed42790_1.jpg";
}
)]
it results in a httpBody like this :
description="WhoGives"&images[][container]=“offerImages”&images[][name]=“563f993e4b00ddad7ed42790_0.jpg”&images[][container]=“ offerImages”&images[][name]=“563f993e4b00ddad7ed42790_1.jpg"
And I would like it to be :
description="WhoGives"&images[0][container]=“offerImages”&images[0][name]=“563f993e4b00ddad7ed42790_0.jpg”&images[1][container]=“ offerImages”&images[1][name]=“563f993e4b00ddad7ed42790_1.jpg"
As anyone found how to do it? And if so, how?
You will need to use a .Custom parameter encoding to do this. Since there's no RFC spec published around collection URL encoding, Alamofire uses a common convention which works for many servers, but not all.
Here's a snippet from our ParameterEncoding docs.
Since there is no published specification for how to encode collection types, the convention of appending [] to the key for array values (foo[]=1&foo[]=2), and appending the key surrounded by square brackets for nested dictionary values (foo[bar]=baz).
To implement this, you'll want to leverage the logic in the encode method for the .URL case along with the queryComponents method. You'll need to make a slight change to the queryComponents method to put the array index in the parameter value. Then you should be good to go.

How to POST / PUT edge with number property?

I am using Rexster to load data into a TitanDB. When posting / putting vertices, I can provide properties as JSON in the request's body. If a property's value is a number, it will correspondingly be stored as a number and can be retrieved as such. For example, the following body will in a post message will create a property "score" of type number:
{
"score": 5
}
When POSTing / PUTing edges, though, it seems properties can only be provided as query parameters, e.g.:
POST .../graphs/graph/edges?_outV=256&_label=review&_inV=512&score=5
In this case, unfortunately, the 5 is always considered as a string: "5". Consequently, queries including numeric operations / comparisons do not work. For example, the following query will still return the posted edge (despite the posted score being 5):
v(256).outE('review').filter{it.getProperty('score')>9}
Is there a way to POST / PUT edges and their properties so that the number type is considered?
I was reasonably sure you could POST JSON to the edge route, but even if you can't, you can use Rexster's explicit type system to post your integer properly:
$ curl -X POST "http://localhost:8182/graphs/tinkergraph/edges?_outV=1&_inV=2&_label=knows&score=(i,5)"
{
"version":"2.7.0-SNAPSHOT",
"results": {
"score":5,"_id":"0","_type":"edge","_outV":"1","_inV":"2","_label":"knows"
},
"queryTime":31.79554
}

Why can't I get HAL support to work in grails 2.3.8?

I am following the directions in the docs, here:
http://grails.org/doc/2.3.8/guide/webServices.html#hypermedia
Why won't grails produce HAL-formatted output, as shown in the documentation?
I have a domain object which I have mapped with the #Resource annotation:
#Resource(uri='/documentCatalogs', formats = ['json', 'xml'], readOnly = true)
class DocumentCatalog {
String entityType
String actionCode
...
}
...and in my conf/spring/resources.groovy, I have configured the HAL JSON renderer beans:
import com.cscinfo.platform.api.formslibrary.DocumentCatalog
import grails.rest.render.hal.HalJsonCollectionRenderer
import grails.rest.render.hal.HalJsonRenderer
// Place your Spring DSL code here
beans = {
halDocumentCatalogRenderer(HalJsonRenderer, DocumentCatalog)
halDocumentCatalogCollectionRenderer(HalJsonCollectionRenderer, DocumentCatalog)
}
Using the debugger, I confirmed that the initialize() method on HalJsonRenderer is called and that it is constructed with the correct targetType.
I send a rest call using Postman:
http://localhost:8080/formslibrary/documentCatalogs/3
Accept application/hal+json
And I get back a response which is regular JSON and doesn't contain any links:
{
"class": "com.cscinfo.platform.api.formslibrary.DocumentCatalog",
"id": 3,
"actionCode": "WITH",
"entityType": "LLP",
...
}
What did I miss? Is there some plugin or configuration setting I have to enable for this behavior? Is there some additional mapping property somewhere that's not documented?
Figured it out! There are multiple aspects of the fix...
I had to add "hal" as one of the listed formats in the #Resource annotation:
#Resource(uri='/documentCatalogs', formats = ['json', 'xml', 'hal'])
Some hunting around in the debugger revealed that Grails will blithely ignore the Accept header, based on the UserAgent string that is sent from the client. (In my case, since I'm using Postman, it was the Google Chrome UA string.)
One workaround for the Accept header issue is to add ".hal" to the end of the URL:
http://localhost:8080/formslibrary/documentCatalogs/3.hal
This isn't a very good solution IMO, since the HAL URLs generated by the renderer don't end in ".hal" by default.
A better solution is to fix Grails' handling of the accept header by updating the config. In Config.groovy, you will see a line that says:
grails.mime.disable.accept.header.userAgents = ['Gecko', 'WebKit', 'Presto', 'Trident']
Change it to:
grails.mime.disable.accept.header.userAgents = ['None']
This forces Grails to honor the Accept header, regardless of the user agent.
Hope this helps somebody else who's hitting the same issue.
P.S. It's really helpful to put a breakpoint in the ResponseMimeTypesApi#getMimeTypesFormatAware(...) method.

How to send xml/application format in bottle?

If some one come to my url suppose /get it should give back a xml/application format in response in bottle framework. How can i do this? i am using elementree as xml generator.
Look on the official page for the cookie example and do it like this:
#route('/xml')
def xml():
response.headers['Content-Type'] = 'xml/application'
....(create the xml here)......
return xml_content_whatever