Beego get response body in middleware after processing - json

I have to append parameter in JSON response after request processing is completed and before sending. I can do in after exec filter. Here I defined filter like this,
beego.InsertFilter("*", beego.AfterExec, AddRequestAfterExec, false)
Now in this AddRequestAfterExec method, I am unable to read JSON response. I need to read JSON response and add parameter to it. I searched many things, but did not find any thing useful. So far I have left it empty.
func AddRequestAfterExec(c *context.Context) {}
Can anybody help, how to read Response in this function and modify it?
Also I am sending response from API controller like this
authController.ServeJSON()

Related

angular 4 http get request doesnt subscribe to json response (mongoose result) from api

the http client get request to the api, works I see that a json object is being returned with the command
res.send (data) after finding it in Mongoose.
I understand that httpclient now sends standard json format back from the api, however I don’t understand how to subbscribe to this data on the client side. How do I receive the object that is being send back with res.send.
I added an interface to the get request that sets up the variables that tie in with the json object.
However I can’t get the data nor do I get any failures as if the .subscribe is not being called.
I am using the httpclient docs but somehow I can’t get it to work.
Below the get request that I am using “might not be entirely correct” which works but the obj doesn’t get subscribed to
http.get(url, {params})
.subscribe (data => {console.log(data)});
}
ie. I get no error, which I expect as the log can’t read json format?

How to send the output of a GET REST request as body of POST request using Postman

For our application, we are supposed to write the test scripts which test our REST API.
So I have written individual tests in postman in a collection. When ran individually, all tests pass.
Now to run all tests sequentially in a collection, I want the output of the first GET request (the response is an array of JSON objects), to pass as input body to next POST request.
The following code is written as a test in Tests of GET request.
var jsonData = JSON.parse(responseBody);
postman.setEnvironmentVariable("data", jsonData);
postman.setNextRequest("POST request");
I have named my post request as "POST request". But this test is failing.
Please let me know if I have done any mistakes.
You're trying to set the entire JSON as an environment variable instead of property value. Try to change your line like this.
const jsonData = pm.response.json();
pm.environment.set('data', jsonData);
Later, use that environment variable as input to your POST request as {{data}}
Considering your JSON response is something like this.
{
"status": "SUCCESS",
"message": null,
"error": null,
"id": 1234
}
Hope I answered your question. I have recently written a blog on automating API's using postman you can refer here API Automation using postman
Let us know if it solves your problem.
Ah!! Missed this point.
If you want to send the entire JSON as input then use JSON.stringify()
Similar question asked
The data should be set as part of the request for the POST operation.
Within Postman, in the body section for the POST request, you can set the data onto the request, either as a form field value or as the entire request body i.e. raw e.g.
{{data}}
Some additional information and examples are here: https://www.getpostman.com/docs/postman/environments_and_globals/variables

JMeter Groovy convert JSON response to string

I've been looking through various solutions for converting JSON responses to string, but I'm still getting something wrong.
I'm trying to read the response from a post request, convert it to a string and modify it (using groovy), then pass it on the next request. The problem seems to be in the groovy script, it's not reading in the response from the first HTTP request and I'm not sure how to fix that.

Parse.com cloud httpRequest response.text does not convert to JavaScript object

I have a http request I am trying to make on an afterSave method in my Cloud Code. I have been able to create my request, and when I console.log(response) it outputs a block that contains the information that I am after. I am aware that response.text is a string so I am trying to run JSON.parse(response.text) so I can access my API response.
I can print out what appears to be an object after running JSON.parse, but much of the data within my response is stripped out. I know it is not the fault of the API because I have another function that runs on the client with the same query and it works correctly.
What is the correct way to parse the response.text from a Parse.Cloud.httpRequest to maintain my data.
Try var result = JSON.parse(response['text']).

Are JSON APIs supposed to return strings or JavaScript Objects?

Let's say I ask Mailchimp for subscriber data, and they send back an http request with JSON in the body. Should I be able to go:
var thingy = body.property;
Or should I have to go:
var object = JSON.parse(body);
var thingy = object.property;
?
Also, does the node.js body parse parse JSON for me?
JSON is sent over the wire from the server as a string. That's what JSON is - a string format.
Whether or not it arrives at your code as a string or as already parsed Javascript object depends entirely upon the code you are using to make the http request and perhaps what headers the server sets and what auto-detection the code doing the Ajax call makes.
If the response header sets the type to json, then some code making the request will automatically parse it for you into Javscript. Other code will leave that to the caller to do. If the server does not set the proper headers, then some code will auto-detect it as JSON and parse it and other code will not.
So ... bottom line. It depends entirely upon what the server is doing in its response and what code is being use to make the request. You can very easily just do a console.log(body) and see whether you have a JSON string or an already parsed Javascript object.
If you really weren't sure what behavior you would get, you can test the type and act accordingly (though a given server and calling code should be consistent so you shouldn't have to vary your behavior) once you test how it behaves.
if (typeof body === "string") {
body = JSON.parse(body);
}
Depends on the API, usually you get the response header Content-type: application/json. If that is the case there's probably no need to parse the response as most of the clients will understand that it's a json object and parse it for you. Anyhow, not all clients will do this automatically.