Direct POST into URL not working? - html

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.

Related

Get Request don't gives any value back in Powerapps Custom Connector

I build a custom connector in PowerApss. I use two requests. One is Post it's working fine, but the other one is not working. It is a get request.
I use the following string. It don't gives any error back, but I also shows no text in the Powerapps text field:
ClearCollect(colMetadata; ForAll(Table(ParseJSON(MetadatenserviceTest.GETBYMandantId("UserAccount"; "application/json"; "application/json").key));{ a: Key }))
Maybe you can help me to solve the problem.
Best regards
Matthias

Ajax Response Json data, are not always valid

My website is using ajax calls to add products to cart. Each time a customer presses "Add to Cart" button, there's an ajax request called. The Json data response is sometimes not valid or not formed correctly.
Using firefox developer tools, here's the response data in both ways:
Normal json response:
Not valid json data response:
1) What kind of issue is this?
2) Why is this happening in some cases and not other cases? Could it be the data itself causing this?
3) Possible solutions to this?
In general there are two possible cases where browser can't parse JSON data:
Wrong Content-Type header
Malformed JSON string
In your case, as it sometimes works and sometimes doesn't it's probably the second one. There must be some characters in your response that are escaped in your server side code which are not valid in browser. All server side language have options when converting objects to JSON strings. you can check the invalid response in a JSON linter like https://jsonlint.com/ to see which part causes the problem then search for options to disable this behaviour in your server side code.
1) What kind of issue is this?
Server side issue.
2) Why is this happening in some cases and not other cases?
Bad logic in the server side backend code.
Could it be the data itself causing this?
No
3) Possible solutions to this?
Fix the server side code's logic.
You should check if the datatype of your Ajax function is JSON and you should check your server side code, perhaps the response is not well formatted.
the problem in you post parameters sometimes sending value or sometimes not check the code of javascript and server code as well for validation.

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)

Contact API directly from URL in browser

I am trying to understand how POST and/or GET methods work in terms of the actual browser.
I am attempting to contact an API which requires API key, method you wish to use on their side, and an IP address at the minimum.
My original idea was to do something like this:
I feel like I'm on the right track, it does something and gets an error as opposed to telling me the page does not exist. I'm expecting either JSON or XML in response as the API supports both but instead I get this error:
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.
Upon studying the documentation of the API more, I found something saying that methods are called using HTML form application/x-www-form-urlencoded and the resuource models are given as form elements.
I tried researching what that means to see what the problem was and found this site http://www.w3.org/TR/xforms11/ but I'm still unclear.
Ideas?
It seems to mean that the application is expecting a POST method but you're doing a request with a GET method (when you use the querystrings).
Since you can't just do browser requests using POST using the address bar, you may need to:
Construct a simple JS function that does a xmlhttprequest request using that method instead, and running it from the console;
Create a simple HTML page that automates the above process, allowing you do make POST calls;
Using CURL instead, which is a great tool for testing those kinds of requests.

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.