How does one enforce HTTP responses as JSON messages? - json

I am quite new to json and webservices.
I have a question which might as well be a dumb one but I have not been able to find an answer.
Lets say I have a html form which performs a GET method on submission.
The server performs an HTTP response.
How does one make sure that the content(body) of the response is in JSON format?
-V

This is quite a vague question as I don't know what your server is doing and replying (obviously it should be in the JSON format response) but you should also include a Content-type HTTP header as shown below so the browser knows it is receiving JSON:
Content-type: application/json
Your JSON response should look like this (taken from Wikipedia):
{
"firstName": "John",
"lastName": "Smith",
"age": 25,
"address": {
"streetAddress": "21 2nd Street",
"city": "New York",
"state": "NY",
"postalCode": 10021
},
"phoneNumbers": [
{
"type": "home",
"number": "212 555-1234"
},
{
"type": "fax",
"number": "646 555-4567"
}
]
}

data=response_from_the_page;
try {
json = $.parseJSON(data);
} catch (e) {
// not json
}

Related

Amadeus flights API error: carrier code is a 2 or 3 alphanum except YY and YYY

I am using the following SDK to search for and purchase flights via Amadeus:
https://github.com/autotune/amadeus/pull/1/files
This was a previously abandoned project I have decided to take on and make work. As part of that project I am trying to purchase a ticket in the sandbox environment and getting the following error:
{
"errors": [
{
"code": 477,
"title": "INVALID FORMAT",
"detail": "carrier code is a 2 or 3 alphanum except YY and YYY",
"source": {
"pointer": "/data/flightOffers[0]/itineraries[1]/segments[0]/operating/carrierCode",
"example": "AF"
},
"status": 400
}
]
}
Here is the json data being sent:
{
"type": "flight-order",
"travelers": [
{
"id": "1",
"dateOfBirth": "1990-02-15",
"name": {
"firstName": "Foo",
"lastName": "Bar"
},
"gender": "MALE",
"contact": {
"emailAddress": "foo#bar.com",
"phones": [
{
"deviceType": "MOBILE",
"countryCallingCode": "33",
"number": "5555555555"
}
]
}
}
],
"ticketingAgreement": {
"option": "DELAY_TO_CANCEL",
"delay": "6D"
},
"remarks": {},
"operating": {
"carrierCode": "UA"
}
}
Any help appreciated!
The error suggests that the sent payload is invalid. I'd advice you use a tool like Curl or Postman to verify you're using the right API documentation, before debugging actual code.
After further reading your PR and checking the API reference at :
https://developers.amadeus.com/self-service/category/air/api-doc/flight-create-orders/api-reference
I think you need to confirm that the Carrier code being passed is available in the segments under:
flightOffers > itineraries > segments
Although the API reference doesn't have operating > carrierCode like you used in the data sent, my guess after seeing the API error response you shared is that they are performing a check against the flight offers passed.
I suggest you check the results gotten when you call the flightOffers and also add it to the payload sent to the sandbox.

How to check Post request of this format using Postman

I am checking json object post request using postman.The data is in format
{
"user": {
"first_name": "Satyam",
"last_name": "Gondhale",
"username": "satyam#gmail.com",
"email": "satyam#gmail.com",
"groups": [],
"is_active": true
},
"phone": "9028571487",
"address": "Pune"
}
Every time I am sending request I am getting error in postman is
{
"user": [
"This field is required."
]
}
How to send this fields as a Post request using Postman to Django server
Start Postman
Click on New.
Select New Request
Change the Method from Get to Post
Set the URL you are using in the rightside of the field POST.
Then navigate to the Tab Body and paste your Code:
{
"user": {
"first_name": "Satyam",
"last_name": "Gondhale",
"username": "satyam#gmail.com",
"email": "satyam#gmail.com",
"groups": [],
"is_active": true
},
"phone": "9028571487",
"address": "Pune"
}
See picture for example:
I think you also need to add a Header.
Navigate to the tab Headers
in the field Key add Content-type
in the field Value add application/json
Click then on Send.
then you should get a response.
Voilaa!
Regards
Xsi

How to handle assertion property content for differenrt responses?

I would like to handle assertion property content according to below responses, for both success and failure.
Success response:
{
"task": "userLogin",
"response": {
"code": 0,
"status": "success",
"error_message": "",
"success_message": "",
"data": {
"userId": "65",
"username": "indian",
"email": "test#gmail.com",
"token": "b0aef6139ffdc1041e01f7587a0dcf61",
"userType": "trial",
"profile_picture": "test.png"
}
}
}
Failure response: will have only the data node missing, and remaining nodes will be present.
"data":
{
"userId": "65",
"username": "indian",
"email": "test#gmail.com",
"token": "b0aef6139ffdc1041e01f7587a0dcf61",
"userType": "trial",
"profile_picture": "test.png"
}
You can use a very simple XPath assertion, keeping in mind that internally SoapUI converts everything into XML representation.
XPath:
exists(//*:data)
Expected:
true
Update based on your comments.
Your original requirement was not clear. The node is present it is just empty. In that case the XPath assertion would be:
empty(//*:data)
Expected:
false
Handy XPath reference.

parsing JSON object to get filelocation

i am new to JSON. Here i am facing problem how to get file location from JSON object, i am using javascript to parse the json
{
"_embedded": {
"binaries": [
{
"fileLocation": "http://images.clipartpanda.com/sports-equipment-clipart-black-and-white-soccer-ball-hi.png",
"username": "testuser3",
"description": "The company required the 28-year-old's help on a matter the directors felt could affect the share price: its Wikipedia page. Short, uninteresting .",
"createdAt": "2015-02-01T21:47:07.000+0000",
"updatedAt": "2015-02-01T22:42:16.000+0000"
},
{
"fileLocation": "http://images.clipartpanda.com/sports-equipment-clipart-black-and-white-soccer-ball-hi.png",
"username": "Sumanth",
"description": "Sample",
"createdAt": "2015-02-23T21:37:13.000+0000",
"updatedAt": "2015-02-23T21:43:11.000+0000"
},
{
"fileLocation": "http://images.clipartpanda.com/sports-equipment-clipart-black-and-white-soccer-ball-hi.png",
"username": "as",
"description": "as",
"createdAt": "2015-02-02T22:46:00.000+0000",
"updatedAt": "2015-02-27T22:06:18.000+0000"
}
]
}
}
i want to read all file locations from JSON object .
Can anyone help me. Thanks in advance
The way you are trying to access fileLocation looks right:
data._embedded.binaries[1].fileLocation
Before that, to parse your data use:
parsed = JSON.parse(data);
Here's a fiddle: http://jsfiddle.net/01aL5upc/
Note that I did remove whitespace and the apostrophe in 28-year-old's in the fiddle, to put it in to a string rather than an ajax request.

Wrong encoding in json response jmeter

via Jmeter I get the json response:
{
"status": 0,
"response": [ {
"id": "123456789",
"login": "xxxxxxxxxxxxx",
"email": "xxxxxxxxxxxxx#xxxxxxx.xxxxx",
"system": "portal",
"firstName": "ÃÂûõúÃÂõù",
"middleName": "ÃÂðÃÂøûÃÂõòøÃÂ",
"lastName": "ÃÂþÃÂõÃÂþò"
}]
}
How can I solve this problem of wrong encoding for this response? The text is russian.
Try with last nightly build (upcoming 2.10) it should be ok, if not report a bug.
2.10 should be released in few weeks from now.