GET parameter being received on server without quotes - java

I am working on a web application where on clicking a button on the UI a GET request is made to the server as below:
https://mywebapp.com?info=%5B%7B%22first%22%3A%22abcd%22%2C%22second%22%3A%22efgh%20ijkl%22%2C%22third%22%3A%22mnop%22%7D%5D
Basically the value I am passing as info is:
[{"first":"abcd","second":"efgh ijkl","third":"mnop"}]
However, when I read this passed value on server, I found it to be received as:
[{first:abcd,second:efgh ijkl,third:mnop}] i.e. all the double quotes are removed.
Now when I try to parse it into json, it fails.
Could you please suggest how could I fix the issue so that the json is received as expected.
Please note that it is an existing big application and I can't change any server level settings.
Thanks

To keep the double qoutes as is you have to send the json in single qoutes
i,e convert the json into string then send it, because the GET/POST request don't recognize json format

Related

Why does my POST to Sagepay returns an Error 400?

I am trying to integrate my application with Sagepay, using the Server Integration Protocol. I have written my code in JAVA and currently I am at the point where I'm sending a POST to Sagepay to be redirected to their payment page. However, I get a blank screen which is a result of an Error 400 (Bad Request).
In their documentation, they specifically state that:
The data should be sent as URL Encoded Name=Value pairs separated with & characters and sent to the Sage Pay Server URL with a Service name set to the message
type in question.
The URL that I have constructed is this:
https://test.sagepay.com/gateway/service/vspserver-register.vsp&VPSProtocol=3.00&TxType=PAYMENT&Vendor=foovendor&VendorTxCode=foovendor-1459865650735-78597&Amount=10&Currency=GBP&Description=This+is+the+description&NotificationURL=http%3A%2F%2Fwww.google.com&BillingSurname=foosurname&BillingFirstnames=fooname&BillingAddress1=fooaddress&BillingCity=foocity&BillingPostCode=foopc&BillingCountry=UK&DeliverySurname=fooname&DeliveryFirstnames=foosurname&DeliveryAddress1=fooaddr&DeliveryCity=foocity&DeliveryPostCode=foopc&DeliveryCountry=UK&CustomerEMail=foo%40foo.com
What am I missing?
Thanks for your help!
Your url doesn't setup the query string properly.
Ithink that
register.vsp&VPSProtocol
should be
register.vsp?VPSProtocol
I.E. Question mark instead of ampersand.
Also, you said a post was required, but pasting that url in a browser will send a GET request, won't it ?

LibGDX HTTP Post request recieving 400 message on Node.js server

I'm working on a game using the LibGDX library. One part of the game involves collecting game data and sending it to a server as a JSON array to be recorded in a database. I'm using Node as my server but I'm running into an issue every time the game sends a POST request to the server. I'm using LibGDX's Http.Net library to send the request.
I keep getting a HTTP 400 error message and data isn't being recorded. Attached are screenshots of the relevant code and messages. Thank you!
Images: https://imgur.com/a/CF1U6#0
I don't have enough reputation to insert images, sorry.
I figured out the problem. I was using LibGDX's included JSON library to construct my JSON String. However, when I created Json json = new Json();, it defaults to writing minimal (I think). Names are not surrounded by double quotes in this format. See: https://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/utils/JsonWriter.OutputType.html
The solution is to set it to Json json = new Json(JsonWriter.OutputType.json);. This will format it as JSON which Express will recognize.

How can I accept JSON as string in Jersey

I am trying to maintain some logs of a couple of Javascript objects in my webapp. The easiest way to log them would be to stringify them and put them on a jersey path as a string.
My logger works fine with regular strings but gives Error 400: The request sent by the client was syntactically incorrect when I pass a JSON stringified object. There are two things that I can't explain and are going wrong with my code:
Everything seems to be working fine on my development server but not on the server where I am deploying it. I develop on a Mac / Homebrew / Tomcat enviroment and deploy on a CentOS server.
Even on the CentOS server, logging works fine when I pass a simple one word strings as message but passing a JSON string throws up the error.
My Logger code looks like this:
#PUT
#Path("logEvent/{fn_event}/{fn_message}")
public void logEvent(#PathParam("fn_event") String event,
#PathParam("fn_message") String message)
throws Exception {
:
:
:
}
I have tried investigating catalina logs but it doesn't tell anything. Access logs give no more information than specifying "Error 400".
This may happen if you don't escape quotes in your JSON string. Try to escape it with \"
Json String may be having "Spaces" etc etc.
So when you call http://yourserver/logEvent/oneword/onewordMessage it may work fine but when you call http://yourserver/logEvent/oneword with space and with & and so many things/or one message with " and not " etc
Then in second case, your path may be incorrectly encoded. Form Encode your json stream, and then pass it as path.
Better move to Post, and pass the stream as Body of Request. Not sure why you will prefer using "entire" json file as path of your service

Server side fix for receiving string containing '&'(ampersand)

We have already shipped a client (.NET WinForms) application which sends customer data to Java server. While most of the data sent by client are accepted at server side, some records are truncated because of the presence of & character in it, as client sends raw & and do not URL encode it, we have fixed it by using the below code:
string dataBefore="A & B";
string dataBefore = System.Web.HttpUtility.UrlEncode(dataBefore);
It is impossible for us to update all the client applications(which are already shipped) and we are thinking of a server side fix.
With the help of Fiddler, we have made sure the data has left client in full, but when server reads as below:
//in java
String dataReceied=request.getParameter("data");
it gets truncated if data contains &
Could someone help us suggesting a server side(java) fix for this? Is it possible to access the request stream in java(instead of request.getParameter())?
You can get access to the raw query string using HttpServletRequest.getQueryString() (javadoc), which:
returns a String containing the query string or null if the URL contains no query string. The value is not decoded by the container.
You can them perform manual decoding on that string, instead of using getParameter().
#Wesley's idea of using getParameterMap() may not be useful, because you don't know which order the parameters were supplied in.
I'd suggest implementing this logic as a servlet filter, to decouple the fixing of the broken parameters from your actual servlet logic. This would involve writing a custom subclass of HttpServletRequestWrapper which overrides getParameter() and manuyally decodes the query string. Your servlet would then be able to use the HttpServletrequest API as though everything was tickety boo.
It is cut off because & signifies a new URL parameter in a request like this:
google.com?query=java&page=2. Java converts all these parameters to a Map, so that's where it goes wrong.
Have you tried iterating through request.getParameterMap()? The remaining data is most likely in the name of the next parameter. If that does not work, check out the API of HTTPServletRequest to see if there is another way to get your data.
Good luck!
PS How angry are you guys at the intern that wrote & shipped that client? That sounds messed up!

How to send JSON from android to Servlet

I get the JSON of User Info from Facebook on Android.
And then send the JSON to my servlet (the platform is GAE).
My question is how to send it properly.
Since the JSON could be very long.
So far, I have tried this way.
But I cannot receive the entire JSON.
It always throws
Unterminated string at character 117 of
{"music":{"data":[{"created_time":"2011-05-23T16:47:21
0000","id":"176345129540","category":"Musician/band","name":"
And I print the JSON, I find that the JSON is just as above which is been cut.
Thanks in advance.
There might be a limit on the size of data you can send by Http GET, take a look at this question and rewrite your request to use Http POST.

Categories

Resources