Postman request encoding [closed] - java

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I'm writing my own HTTP server. I'm using Java Socket for it. I read request from InputStream next way
val input = BufferedReader(InputStreamReader(socket.inputStream, "UTF-8"))
When I receive requests from curl or browser all is good. But when I receive request from Postman I get something like this:
��g�������#���=��g�������#���, ��9� ��3���5�/�=��9� ��3���5�/�,
I tried to use others encodings for InputStreamReader such as UTF-16 and ASCII. Which encoding is used for Postman requests and how I can read it on my server?
UPDATE: Sorry, this is my failure. I used HTTPS when making requests.

If the first two bytes received are ASCII 31 (0x1F) and 139 (0x8B) you are receiving a GZIP stream and for some reason convinced the client that you supported GZIP Content-Encoding (not the same thing as the charset encoding).
The data looks more binary than character encoding. Gzip, deflate, SSL, or other reason for binary data is what should be looked at.
You may not be doing content negotiation correctly and therefore receiving binary gzip or deflate. Or using HTTPS vs. HTTP. Or uploading an image. Something but not text.

NOTE: my other answer is more direct, this is related HTTP server libraries for use in Kotlin, in case others read this question and want to know alternatives. Not sure if this is an XY problem and if the author is not aware of other options.
Other ways if you do not want to build an HTTP library, but instead use one:
Raw HTTP frameworks:
Anything written for Java, and specifically:
Undertow.io
Vert.x 3 with Vert.x-web
Netty - very low level
Somewhat more than raw HTTP, but not a MVC or REST framework:
Wasabi - Kotlin specific
And for fuller REST or Web frameworks:
KTOR - Kotlin specific
Kovert - Kotlin specific, REST but adding views (Disclaimer: I'm the author)
Spark Java - Java, works nicely in Kotlin
Vert.x Nubes - Java, to work on top of Vert.x
Kikaha - Java, to work on top of Undertow

Related

Android Studio how to connect my project with mySQL db (news feed app) [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I have searched a lot for the answer but could not find it. Please help me understand how to connect my android app to mySQL.
Assuming you mean to have your Android application talk remotely to a server hosting your news feed source (articles, etc.) then you might want to have a look at creating a RESTful service (a basic example of how to achieve this can be found here).
You'll want to be familiar with:
SQL (http://www.w3schools.com/sql/)
JSON (http://www.w3schools.com/json/)
RESTful services
(http://www.drdobbs.com/web-development/restful-web-services-a-tutorial/240169069)
Basically what would happen is whenever your activity needs to display news feed results to the user, it can query your RESTful backend (via an http POST/GET request) and wait for your web service to respond. Your web service will listen for HTTP requests matching a specific URL, for example, mynewsfeed.com/get_articles, and upon getting this request, your web service will issue an SQL query against whatever database contains your articles and return the results in an HTTP response.
I'd personally recommend sending data back and for between the server/client using JSON (unless you have a reason for using another format.) This way, when your Android application gets the response from the server, it can parse the JSON data for various attributes contained in your article (title, author, content, etc.)
Google's GSON library has a neat way of converting JSON data into a Java object, so you don't have to worry about manually parsing each JSON field out yourself: https://sites.google.com/site/gson/gson-user-guide.
As for how to actually build the web service, there are tons of frameworks out there. I've personally used Flask (http://flask.pocoo.org/docs/0.10/tutorial/) in the past and find it very lightweight, straightforward and supported by pretty decent documentation. You'll also need somewhere to host the webservice and store the SQL database (for this I pay ~$30 a year for a simple hosting service and a domain.)
Hope this points you in the right direction (there is a lot of material here.) I'm happy to provide clarification if what I've posted isn't clear.

Java: http request, print headers and parse json

I have no knowledge of Java, but a collaborator is writing code and attempts to parse a JSON response that the server that I am writing is providing. I also created this question yesterday trying to see if there was a problem on my end. However, the problem still remains.
So, can someone give me some simple code of what needs to be written in Java in order to make an http request to a server (say http://www.google.com) print the headers of the response and moreover parse a json object that is returned? Or at least, which functions should I be using and which libraries I should include in the source code. I want to believe that I will figure it out after that point.
I am totally clueless how to do that and what sort of libraries I need in java. Honestly, I do not know java.
Your first step would be looking into the Servlet specification of the Java language. I would recommend the Google GSON library for parsing JSON.

why database url has to be in a specific format in jdbc? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
As mentioned in oracle documentation, the database url needs to be in format "jdbc:subprotocol:subname: in DriverManager.getConnection() function. The first and most obvious question is why not such a simple way as, for example, "C:\myDatabase.db"?
If you split each component in the URL, you can understand why we need it in that format.
there are many databases each with it's own proprietary way of storing information. And only a specific driver can understand and interpret that proprietary information. So, we need the subprotocol to be oracle/mysql/etc.
there are may type of drivers just for oracle. Please check http://docs.oracle.com/cd/E11882_01/appdev.112/e13995/oracle/jdbc/OracleDriver.html where you can have thin/oci/etc which go into the subname.
As Sotirios mentioned, the case where the database is not in your C:\ and is in somebody else's server - so we need a host, which then naturally mandates for a port as well.
Even in that server, the same port, one database 'server' can provide services for multiple databases (or services). So, we need the database name in the URL as well.
Java is operating system agnostic and a format such is "C:\myDatabase.db" is operating system specific. It would not work, for example, to connect to an Oralce DBMS running on Solaris. They are also trying to make it DBMS independent. Not all DBMS systems use the same database/table organization of systems like MySQL. Considering the complexity of the problem trying to solve, their solution is actually rather simple.
The JDBC-Url is an URL an Uniform Ressource Identifier. This means have to follow a syntax.
You can rewrite C:\myDatabase.db to an URI too file:/c:/myDatabase.db.
In fact for the Windows-Explorer booth are the same, but a webbrowser can only understand the URL-form.
Back to URL:
An url follows a syntax. The first part is the protocol (file)
This will be separated by a : from the prootocoll specific part.
For file-URIs, this will be a Path starting with the root-directory / followed by folders separated by /
For JDBC-Urls, the only common part is the scheme (jdbc) and the separator :.
In your example you use a local file. But how would you specifiy a database running on another server? You will have to create another way. So you may start like this:
jdbc: the schema
mydatabasesystem: this may say "This is an JDBC-Url for 'ydatabbasesystem' and I know the following syntax.
localfile: I will use a local file and the followin part is the syntax for localfiles
/c:/myDatabase.db There is my file
This will lead to a jdbc-Url:
jdbc:mydatabasesystem:localfile:/c:/myDatabase.db
Imaging your database may be connected via network.. This may be written like:
jdbc:mydatabasesystem:remoteserver:servername?username=user&password=secret
You see, if you write a generic way to connect to databses of different manufacturers, youwill have to create a common wy to connect to the database. This will lead to something like a JDBC-url.

Most performant protocol for uploading binary data to a jax-rs (REST) service

Environment
My application (war) has a JavaScript frontend and a Java REST service.
The files to be uploaded will be generated in the frontend, but not directly by user interaction -- this is not a use case where the user is uploading files herself. For that reason, it's necessary to initiate the upload from the JavaScript code.
I need to be able to send metadata (generated by other parts of the application) about the binary data when I'm uploading it -- which is why I need some sort of protocol instead of just uploading a file.
Question
What I haven't been able to determine is what the best practice is for uploading files, with regards, primarily, to the protocol used.
I've come across the following protocols:
json
xml
proctol-buffers (via protobuf.js)
However, the internets has, as usual, lots of different info that hasn't been giving me a coherent picture:
With regards to reliability, the internets seems to say that you're better off using the multipart/mixed type to transfer data, instead of the pure application/octet-stream type.
json doesn't natively doesn't support binary data, and apparently, Base64 has a high processing overhead.
It's a JavaScript frontend, so json would be preferred.
Sure, I could use protobuf.js, but I'd rather use leading-edge tech than bleeding-edge tech.
My priorities are:
reliable data transfer of files between 1 and 10 megabytes.
performant and efficient data transfer.
readable code/architecture
In short, which of the 3 formats mentioned above fits those requirements the best, given that I'm using a Java REST service on the backend?
(If the fact that I'm using a Java REST service -- instead of say, a servlet -- to upload the files is going to be the biggest slowdown, that's also a good answer!)
EDIT: added information asked by the comments -- thanks!

Ways to test RESTful services? [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 9 years ago.
Improve this question
I want to test my RESTful applications directly via HTTP and I am looking for tools that can help me with that task. Basically I am looking for an easy wrapper for HTTP requests that can submit e.g. HTML forms or serialized resources as JSON or XML.
It would be great if there is a way to verify if the service is actually following REST architectural guidelines (statelessness, URIs, content negotiation etc.), too.
Being able to use it with JUnit would be a convenient bonus. Do you know about any libraries that could help me with what I want to do (and that are a little more than just a simple http client)?
See if rest-client is of any help.
Edit: Currently I am using Postman - REST Client a google chrome plugin and it's awesome!
I think REST Assured will suite you very well. It's very easy to send requests and to parse XML and JSON responses. E.g. let's say that a GET request to "/lotto" returns JSON:
{
"lotto":{
"lottoId":5,
"winning-numbers":[2,45,34,23,7,5,3],
"winners":[{
"winnerId":23,
"numbers":[2,45,34,23,3,5]
},{
"winnerId":54,
"numbers":[52,3,12,11,18,22]
}]
}
}
You can make the request and validate the response like this:
expect().body("lotto.lottoId", equalTo(5)).when().get("/lotto");
There is also the Jersey Test Framework (http://jersey.java.net/nonav/documentation/latest/user-guide.html#test-framework) but as Johan already mentioned the REST-assured framework I'd also recommend this framework - it has some nice featues like a DSL like syntax, XPath and Schema validation, easy file upload and using Groovy Lambda Expressions to search through returned JSON structures..
I have written two articles..
the first one compares REST-assured and Jersey-Test-Framework (http://www.hascode.com/2011/09/rest-assured-vs-jersey-test-framework-testing-your-restful-web-services/),
the second explores the features of the REST-assured framework against a given REST service (http://www.hascode.com/2011/10/testing-restful-web-services-made-easy-using-the-rest-assured-framework/)
Fiddler is a really useful tool, you can create XML based HTTP Requests with a variety of request verbs like GET,POST,PUT,DELETE and so on.
http://www.fiddler2.com/fiddler2/
Maybe Selenium can be of some help, but surely not entirely.

Categories