Grails Consuming JSON response. - ajax

I'm trying to create a restful service and I'm trying to consume my JSON response and convert it back to an Object.
Controller method
def mergeVendors(String region) {
def report = new VendorReport();
//do something with report
response.status = 201
response ([vendorReport: report]) as JSON
}
Test Method
void "Test Merge Vendors"() {
when:
controller.request.method = 'POST'
controller.request.json = '[{id:1, zip:"14224"}]'
controller.mergeVendors("Florida")
def response = controller.response
then:
response.zip == "14224"
}
The code above is what I'm trying to use and I get the following exception. How do I cast the response back to a VendorReport obj?
groovy.lang.MissingMethodException: No signature of method: org.healthresearch.VendorController.response() is applicable for argument types: (java.util.LinkedHashMap) values: [[vendorReport:org.vendor.VendorReport#1b0dbdf1]]
Possible solutions: respond(java.lang.Object), getResponse(), respond(java.lang.Object, java.util.Map), respond(java.util.Map, java.lang.Object), respondsTo(java.lang.String), respondsTo(java.lang.String, [Ljava.lang.Object;)

response ([vendorReport: report]) as JSON
change to
render ([vendorReport: report]) as JSON

Related

Python AWS Lambda's event body returning string instead of my json object

Invocation code
import requests
import json
# Create a new resource
data_object = {'key1':'testing'}
response = requests.post('https://fakeurl.execute-api.us-east-1.amazonaws.com/default/My_Test_Event_Lambda', data=data_object)
print(response._content.decode())
Lambda Code
import json
def lambda_handler(event, context):
return {
'statusCode': 200,
'body': json.dumps(event['body'])
}
The response I get from invocation is "key1=testing"
I don't care so much about the response but i want the lambda function to be able to handle my data passed as json not a string. Example: I want to be able to say event['body']['key1'] and have it return "testing"
Currently API gateways being used as lambda proxy.
The event['body'] you received is a string. You need to parse that from JSON into a dict with:
d = json.loads(event['body'])
Then you can return that dict as the body in your response, if you want, via:
return {
'statusCode': 200,
'body': json.dumps(d)
}
As it stands, you're just managing strings.

Using Ruby server as API middleman

I am trying to call an API from my ruby server and simply pass the response back to my client.
How do I make sure the response is passed back to the client. I am currently getting back an empty response with a status code of 204
class SignupPlayerPagesController < ApplicationController
skip_before_action :authorize
def index
render locals: { signup_player_pages: SignupPlayerPage.all }
end
def show
signup_player_page = SignupPlayerPage.find_by(slug: params[:slug])
return render json: { error: 'Not found' }, status: :not_found if signup_player_page.nil?
render locals: { signup_player_page: signup_player_page }
end
def sms
response = HTTParty.get('http://api.stackexchange.com/2.2/questions?site=stackoverflow')
return response
end
end
The current HTTParty request is a dummy request I just want to know if it works.
which method is returning empty? index, show or sms?
if its sms, the you may need to do something like:
render json: { message: response }
instead of the "return response". Btw the "return response" can be deleted anyway, its redundant.

How to stub a HTTParty request inside a method for testing?

I have created a function that makes a HTTParty get request. It raises a custom error message that i need to test. I tried to stub the request using Webmock in the test but it is raising a <Net::OpenTimeout>. How can i stub the get request if the url is dynamically constructed?
def function(a , b)
# some logic , dynamic url constructed
response = HTTParty.get(url, headers: {"Content-Type" =>
"application/json"})
if response.code != 200
raise CustomError.new <<~EOF
Error while fetching job details.
Response code: #{response.code}
Response body: #{response.body}
EOF
end
JSON.parse(response.body)
for the test
def test_function
WebMock.stub_request(:get, url).with(:headers => {'Content-
Type'=>'application/json'}).to_return(:status => 500)
# HTTParty.stub(get: fake_response)
err = assert_raises CustumError do
c.function(a , b)
end
WebMock allows you to use "wildcard matching" so you can stub requests matching a regular expression:
WebMock.stub_request(:get, /example/).to_return(status: 500)

Subscribing to Angular 6 POST request

Scenario
I have an Angular 6 front end that communicates with a Spring Boot back end. The back end uses PostgreSQL as a database. The thing I want to do is to send a username to the data base and then return his email and print it on a page using Angular 6.
But, I am struggling with the Angular POST request where on subscribing to it I get a JSON parsing error in Chrome developer tools.
Code
Spring Boot
This works fine as I have tested it by printing the email returned from the database on the console.
#RequestMapping(value = "/reset", method = RequestMethod.POST)
public String resetMail(#RequestBody String username) {
try{
User user = userService.findByUsername(username);
//System.out.println(user.getEmail()); Testing purpose
return user.getEmail();
}catch(Exception e) {
//e.printStackTrace(); Prints out NullException StackTrace.
return "Not Present";
}
}
Angular 6
In the following code, I think the problem is in the subscription method as I am getting a JSON parsing error in Chrome developer tools.
httpOptions = { headers: new HttpHeaders({'Content-Type':'text/plain', 'Access-Control-Allow-Origin': '*'})};
reset(username:string) {
this.http.post('http://localhost:8090/reset', username, this.httpOptions).subscribe(data=> {
this.email = data as any;
});
}
console.log(this.email) // Prints undefined
Error
HttpErrorResponse {headers: HttpHeaders, status: 200, statusText: "OK", url: "http://localhost:8090/reset", ok: false, …}
>error: {error: SyntaxError: Unexpected token a in JSON at position 0 at JSON.parse (<anonymous>) at XMLHttp…, text: "abcdefg#yahoo.com"}
>headers: HttpHeaders {normalizedNames: Map(0), lazyUpdate: null, lazyInit: ƒ}
message: "Http failure during parsing for http://localhost:8090/reset"
name: "HttpErrorResponse"
ok: false
status: 200
statusText: "OK"
url: "http://localhost:8090/reset"
You want to serialize a string to JSON. If your controller would return an object the #RestController annotation would serialize it properly. In case you want to return just a string you have to return JSONObject.quote(user.getEmail()); You can get the JSONObject dependency from here http://mvnrepository.com/artifact/org.json/json
But I encourage you to always return objects as a response from your rest controllers.

Spring + Angular: How to parse ResponseEntity in angular?

I'm using Spring Boot to create an API that needs to be consumed in Angular 4. Spring and Angular are on different ports.
The problem is that Spring's ResponseEntity raises an error in Angular.
#RequestMapping(value = "/{id}", method = RequestMethod.GET)
public ResponseEntity getFlow(#PathVariable int id) {
Flow flow = flowService.findById(id);
return new ResponseEntity(flow, HttpStatus.FOUND);
}
Now, I can perfectly use Postman to test the API and it works.
But when I make a request from Angular, it returns an error:
Strangely, it returns an error alongside the requested object.
Now, the cause of the problem is that the Spring Boot application returns a ResponseEntity and not a normal object (like String), and Angular doesn't know how to interpret it. If the controller returns just a Flow object, it works.
How can it be solved using ResponseEntity? Or, how else can I send the object alongside the HTTP status code?
Also, in #RequestMapping put produces = "application/json", and in get request in angular, add http options :
const httpOptions = {
headers: new HttpHeaders({
'Accept': 'application/json',
'Content-Type': 'application/json'
})
};
So your get request looks like this:
this.http.get(url, httpOptions)
As per the document mentioned here
https://docs.angularjs.org/api/ng/service/$http
A response status code between 200 and 299 is considered a success status and will result in the success callback being called. Any response status code outside of that range is considered an error status and will result in the error callback being called. Also, status codes less than -1 are normalized to zero. -1 usually means the request was aborted, e.g. using a config.timeout. Note that if the response is a redirect, XMLHttpRequest will transparently follow it, meaning that the outcome (success or error) will be determined by the final response status code.
As you are sending an instance of ResponseEntity(HttpStatus.Found) whose Http status code is 302 which doesnt fall under the success range thats why error callback is called.
Try returning the content like this
return new ResponseEntity(flow, HttpStatus.OK);

Resources