Using the nasaAPI with the example arguments - json

https://rapidapi.com/dimas/api/NasaAPI?endpoint=apiendpoint_b4e69440-f966-11e7-809f-87f99bda0814getEarthAssets
I've seen the documentation, which gives "50.37, 26.56" as an example of how to format coordinates. The problem is that when I fill in those coordinates in the linked application, it responds with this:
{
"callback":"error",
"contextWrites":{
"to":{
"status_code":"JSON_VALIDATION",
"status_msg":"Syntax error. Incorrect input JSON. Please, check fields with JSON input."
}
}
}
I have a sneaking suspition that its broken, but if anyone knows how I could get it to work, that would be great. Its also worth mentioning that I've gotten other parts of the same API, such as getPictureOfTheDay, to work, and the ones I've gotten to work don't require arguments.

You need to put a empty json object in the post request.
I dont know why they made it a post request

Related

Is it normal for http responses to return inconsistent json

I am working with an endpoint thats returning fields which sometimes return a json object, and sometimes an empty array. For example,
response 1:
{
"status": true,
"people": {
"id":"123",
"name":"john"
}
}
response 2:
{
"status": true,
"people": []
}
Notice that "people" is set to an empty [] when there's nothing, and a singular object when something exists.
I have seen this come up a few times in random places, and it seems a bit cavalier to me as the inconsistency make the values seem ambiguous. I was wondering if anyone knew if this is considered to be bad practice, and why. Would you consider it a bug on their end? Can anyone help shed some light on this please?
Thanks!
Some client technologies will want to deserialize this JSON into an object of some class. Trying to accommodate the two JSON snippets you posted will be difficult for some clients (at least in a statically typed language). A RESTful webservice implementation should not assume that a client will use a particular stack or even the same language as the service. For this reason, if I was designing this endpoint, I would try to avoid returning this inconsistent result.
Of course, as the other answer pointed out, it is a matter of convention and will work. I just think it will make things unnecessarily complicated for some clients trying to work with your service.
Depends, there are a number of stacks that will return a single {} object if there is one result for people and an array of people [person, person] if there are more than one. I personally would prefer an array containing one entity.
If you are saying that people are never more than one (the name implies many) then I'd definitely say the api was inconsistent in returning an array when there are no results and an object literal otherwise.
As to what should happen if there are none? It's a matter of convention and an empty array is no stranger than any thing else, though it could be a null, or something else.
Again, it's a matter of convention. If you have some say in the construction of the api you are calling, you can express a preference. Otherwise you just have to adapt to what comes back.

Pentaho HTTP Post using JSON

I'm brand new to Pentaho and I'm trying to do the following workflow:
read a bunch of lines out of a DB
do some transformations
POST them to a REST web service in JSON
I've got the first two figured out using an input step and the Json Output step.
However I have two problems doing the final step:
1) I can't get the JSON formatted how I want. It insists on doing {""=[{...}]} when I just want {...}. This isn't a big deal - I can work around this since I have control over the web service and I could relax the input requirements a bit. (Note: this page http://wiki.pentaho.com/display/EAI/JSON+output gives an example for the output I want by setting no. rows in a block=1 and an empty JSON block name, but it doesn't work as advertised.)
2) This is the critical one. I can't get the data to POST as JSON. It posts as key=value, where the key is the name I specify in the HTTP Post field name (on the 'Fields' tab) and the value is the encoded JSON. I just want to post the JSON as the request body. I've tried googling on this but can't find anyone else doing it, leading me to believe that I'm just approaching this wrong. Any pointers in the right direction?
Edit: I'm comfortable scripting (in Javascript or another language) but when I tried to use XmlHttpRequest in a custom javascript snippet I got an error that XmlHttpRequest is not defined.
Thanks!
This was trivial...just needed to use the REST Client (http://wiki.pentaho.com/display/EAI/Rest+Client) instead of the HTTP Post task. Somehow all my googling didn't discover that, so I'll leave this answer here in case someone else has the same problem as me.
You need to parse the JSON using a Modified JavaScript step. e.g. if the Output Value from the JSON Output is called result and its contents are {"data"=[{...}]}, you should call var plainJSON = JSON.stringify(JSON.parse(result).data[0]) to get the JSON.
In the HTTP Post step, the Request entity field should be plainJSON. Also, don't forget to add a header for Content-Type as application/json (you might have to add that as a constant)

Direct POST into URL not working?

I am trying to contact an API by posting the parameters in the URL. I am unsure whether it will respond in XML or JSON, but it is one of the two, however, it says there is an error.
This is an example of what I'm submitting. I am receiving this in response:
This page contains the following errors:
error on line 1 at column 1: Document is empty
Below is a rendering of the page up to the first error.
I do not know what is going on... I followed the syntax of the POST I believe, my only remaining question about the syntax would be whether the ? is in the right spot. The page API does work when I POST using PHP...
Or maybe it is working, the browser just isn't capable of understanding an XML or JSON response? (I'm using chrome so I do not think this is the issue)
Otherwise, if anyone has any insight on this, I'd be greatful
A different browser yields this error:
XML Parsing Error: syntax error
Location:
Line Number 1, Column 1:Array
^
While the syntax of the URL does seem to be fine, you imply that the API expects the parameters in POST. Adding them to the actual URL means the parameters are passed in GET, rather than POST.
You could try to test this by making a little HTML form containing all the relevant parameters and passing them to this API via POST, and see if that gives you the expected result.
your issue is how their being sent to the api it should be url-encoded
http://api.example.com/api/?apikey=asdfa23462=example&ip=208.74.76.5
should be
http://api.example.com/api/?apikey=asdfa23462&=example&ip=208.74.76.5
also another issue i see is that you have ?apikey=asdfasfsdafsd&=example
the =example could well be the issue all together.
just some thoughts from what i see.

hard time with JSON and google maps

It might be a frequent question but i cannot figure out how to prevent errors in my parsing when the script can't find a property...
in XML was easy because even the empty properties were like <location/>
but now if location is not available JSON paser cant find it and it results in errors...OR
it may happen the json has different property or a children lost its father..... so for instance if you need to extract the LocalityName is no more under SubAdministrativeArea but under AddressLine...
any of you have any experience about? what the best way to solve it and to parse it correctly?
While answering one of your other question I had written the following javascript code to obtain the lat and lng from the JSON returned by the maps api without any validations for zero results.
$.getJSON("getjson.php?address="+address,
function(data){
lat=data.results[0].geometry.location.lat;
lng=data.results[0].geometry.location.lng;
//.....map initialization code
}
);
Now if I were to validate for zero results, I'd modify the code in the following way
$.getJSON("getjson.php?address="+address,
function(data){
if (data.result.length>0) {
for (count=0;count<data.result.length;count++){
lat=data.results[count].geometry.location.lat;
lng=data.results[count].geometry.location.lng;
//.....map initialization code
}
}
}
);
As you can see parsing JSON comes naturally to javascript and to many other languages for it resolves down to arrays/lists and objects/dictionary/hash.
If I get this right and you are using a library to convert to json like gson try to construct some kind of object array like arrayList in java and then convert to json so every object you retrieve later in javascript is distinct and thus clear during debugging.Also if you don't use firebug give it try as it shows json data clearly.cheers

Averting Cross Domain issues with Callback functions?

I am using HTML and trying to get a JSON response from a URL.
I ran in to cross domain issues.
I then tried using a callback function in order to avoid this problem.
When I do so, and the control passes to the function. I see a "Invalid Label" error in firebug and it shows the JSON response that i get back.
When i did some reading i found a few articles which said the invalid label error could occur because the first word of the JSON response is thought of as a Javascript label and it should be wrapped as a string.
However it did not work because firebug throws the error before even it hits the first line of the function. I also tried debugging in chrome and I get the same result.
Any input would be greatly appreciated.
Found something that might help for you here
Quote from there:
The problem occurs because eval is
interpreting the first item in the
JSON string as a JavaScript Label. The
solution is to wrap the JSON string in
parenthesis.
See this link
I'd suggest using jQuery's .json method to retrieve json, because it hides this implementation.
Thanks for the replies.
I tried the changes and still was facing the same issue. THe solution of wrapping the json string in parenthesis does not solve the problem because this issue is faced even before the code hits that portion.
The problem was that the API was not call back enabled. ( Grrr :() I know! But that was the issue. once the API callback was enabled, the code worked like a charm.