Test jsonp rest app response using httpbuilder - json

Hi
I'm trying to build up a rest Service which provide JSON response.
Yesterday i figured about what is JSONP, why to use it an so and so.
Today i would like to make some test with my new version of my service.
To test it, i use HTTPbuilder. But i can't get it working correctly.
Here is the start of my test:
def client = new RESTClient("http://localhost:"+SERVER_PORT)
def resp = client.get(path:'/music', contentType:JSON);
And here is the error i got:
ATTENTION: Error parsing 'application/javascript' response
net.sf.json.JSONException: Invalid JSON String
How can i help httpbuilder to understand that the response is a JS callback?

Ok i was a littel dumb on this one.
The accepted content type must be ContentType.ANy

Related

How to disable response parsing in Karate?

I'm using Karate 0.9.5. and am testing an endpoint that returns a 125MB json response (I know, shouldn't do that over json -- but am stuck for now). How can I disable Karate from parsing the response json and just treat it as plain text? The response takes milliseconds to come over the wire, but Karate just hangs trying to parse the response. I don't need to validate the response, just check for 200 OK.
Thanks.
You can't. I suggest you write a Java util (should be just 5-10 lines of code) to do a GET or whatever to this end-point and call it from Karate and save to something like target/temp.json or discard the response.
Refer to Java interop: https://github.com/intuit/karate#calling-java
If you use karate-apache then the Apache HTTP Client will be in the classpath. So you can refer to the docs: https://hc.apache.org/httpcomponents-client-4.5.x/tutorial/html/fundamentals.html#d5e49
Also look at the end of this test for an alternative example of making a GET / POST in Java: ProxyServerTest.java

NodeJS Joi Vlidation - How to return JSON response instead of a string?

Recently, Iv'e been using the Joi validation library in-order to validate the data which comes in from a request (building a RESTful API).
I was using the .label() method to generate a string response and send it back to the user, but I couldn't find any way to send a JSON response back to the user?
Tried sending a premade JSON inside the string, searching the documentation and the internet of course - couldn't find any mention of it.
Current code example:
textField: Joi.string().required().max(4).label("This example field didn't pass the testing phase, please try again)"),
Any ideas?
If you need to send data in case of error,try .error() method. you can pass error in it.

API endpoint returns text before and after the JSON content, causing parsing error

I'm using postman to test calling a rest service endpoint.
I'm trying to parse the JSON return content but it throws an error because the response body has more than just JSON.
This is how I parse it in my postman test script:
var jsonData = JSON.parse(responseBody);
Here is the response body:
--13398550-b6ea-4731-a8ee-4b2ad24c3cfe
Content-Type: application/json; charset=utf-8
//this is the actual content I want to parse --->
{"id":"123456","value":"the_value"}
--13398550-b6ea-4731-a8ee-4b2ad24c3cfe--
When I try to parse it, I get the following error (in postman)
There was an error in evaluating the test script: SyntaxError:
Unexpected number in JSON at position 3
Obviously because the content being parsed is not just JSON
Is this something special that the api is doing? Or am I just parsing it incorrectly?
NOTE: I'm not including details of the rest service function. If the cause of this issue is something that is being done by the service itself, then that is enough of an answer for me to perhaps ask another question or do some further investigation. The purpose of this question is to ask whether this is something special being done in HTTP, or if it's the service.
Edit:
I managed to see the server side code and it is indeed manually building the response with boundaries identified by a GUID. I'll have to manually parse the response
The server is not emitting straight up application/json, it's packed in a multipart mime envelope.
Whether or not it's doing that correctly depends on the response headers. If you didn't expect a multipart response, but a simple JSON response, then I'd say yes: it's something you need to fix server-side.

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']).

RestSharp usage for sending and receiving data

I have successfully created my app and now want to connect it to a localhost to check the working of my app. I have been suggested to use restsharp for connecting to the server using php and json to receive data from server.
I have looked at codes for both but do not completely understand how the process works. I have looked into all forums but found could snippets with no explanation as how it works. I have even tried restsharp.org and google search results. Please explain me as to how this works.
RestSharp is a library that helps you invoking REST web services.
You use RestSharp on your client to invoke Rest style Web Services (send and receive data)
Here is an example on the usage of your service:
var client = new RestClient(baseUrl);
var request = new RestRequest("/*rest_resource*/", Method.POST);
// see Rest services
// set the request format - HTTP Content-Type text/xml
request.RequestFormat = DataFormat.Xml;
// add data to the request
request.AddBody("<books><book>RestSharp Book</book></books>");
/* send the request and if your service returns text put the as expected return type; otherwise you will get raw byte array*/
IRestResponse response = client.Execute(request);
//HTTP status code 200-success
Assert.IsTrue(response.StatusCode == HttpStatusCode.OK);
Assert.IsTrue(!string.IsNullOrEmpty(response.Data)); // the response is not empty