Avoid escape foward slash in Android request with JSONObject - java

I'm trying to do a network request with HttpURLConnection and JSONObject, like this.
Step 1: class to serialize the request object
fun <T> GsonBuilder.getEncryptedJSON(request: T): JSONObject {
var encryptedJSON = JSONObject()
try {
var data = this
.excludeFieldsWithoutExposeAnnotation()
.registerTypeAdapter(String::class.java, StringEncryptSerializer())
.registerTypeAdapter(java.lang.Long::class.java, LongEncryptSerializer())
.disableHtmlEscaping()
.create()
.toJson(request)
encryptedJSON = JSONObject(data)
} catch (exception: JSONException) {
Log.e("exception-json", exception.toString())
}
return encryptedJSON
}
Step 2: Class UploadRequest that extends HttpRequest and has a property called body witch is type of JSONObject
uploadRequest.body = GsonBuilder().getEncryptedJSON<UploadRequest>(this)
Step 3: request after a few steps, is the request HTTP class
postData = request.getBody().toString().getBytes(StandardCharsets.UTF_8);
wr = new DataOutputStream(connection.getOutputStream());
wr.write(postData);
wr.flush();
I've seen: After transform my body into JSONObject, the problem happens.
The result I expected is bellow:
{"fileName":""0NKTqOdZ154T/ksxOwdx0VzHNQmGfM0UlpPrreb0vFnGh/Rw3UZEx94aUAuZtr8lW/da07/h/RNoyXpMqpGXKA===="}
But, everytime I've seen the log inside the gateway, the request content is ESCAPING FOWARD SLASHES, like this
{"fileName":""0NKTqOdZ154T\/ksxOwdx0VzHNQmGfM0UlpPrreb0vFnGh\/Rw3UZEx94aUAuZtr8lW\/da07\/h\/RNoyXpMqpGXKA===="}
Do you have any idea why this happen?!
Thank you!

Related

GET/POST Requst to REST API using Spring Boot

I have a REST Service an external server like https://api.myrestservice.com and I have a Spring Boot Application running locally on http://localhost:8080. Now I want to make GET or POST request to the REST API address i.e https://api.myrestservice.com/users to get all users, using my locally running Spring Boot App i.e through http://localhost:8080/users. I am not getting how to redirect local app request to external server request.
I hope I got your question right. You are trying get your local app to get data from app running on your server.
You can use the below sample code in your spring boot application.
private void getUsers() {
final String uri = "https://api.myrestservice.com/users";
RestTemplate restTemplate = new RestTemplate();
Users result = restTemplate.getForObject(uri, Users.class);
System.out.println(result);
}
Then your getUsers can be invoked by getUsers Controller in your spring boot app.
I am adding the reference if you want to look at more examples -
https://howtodoinjava.com/spring-restful/spring-restful-client-resttemplate-example/
Making post Api call from your code to another server:
suppose you have a server https://searchEmployee... which returns you list of employees belonging to a particular city or belonging to a particular organization:
request body:
{
"city" : "Ranchi",
"organisation" : "Bank Of America"
}
json response: [{"name": "Vikash"},{"name":"kumar" },{}...etc]
Then to make a post api call you can use RestTemplate in java like this:
public void makeApiCall(){
final String uri = "https://searchEmployee...";
RestTemplate restTemplate = new RestTemplate();
String reqBody = "{"city": "Ranchi"}";
String result = restTemplate.postForObject(uri, reqBody, String.class);
// convert your result into json
try {
jsonResponse = new JSONObject(result);
} catch (JSONException e) {
e.printStackTrace();
}
//extract a value "name" from your json data:
try{
String value = jsonResponse.getString("name");
}catch(JSONException e) {
e.printStackTrace();
}
}
/********************************************************************/
if you have more than one request body parameters to set do it like this:
String reqBody = "{\"quantity\":100,\"name\":\"product1\",\"ifBoolean\":false}";
false is a boolean value here in your request body and 100 is an integer.
NOTE
if you are having problem in setting request body copy it directly from postman request body and paste it inside double quote.
There are many ways to do it. Like Apache HTTP Components and other. Sample
String type = "application/x-www-form-urlencoded" Or Set your desire content type;
String encodedData = URLEncoder.encode( rawData, "UTF-8" );
URL u = new URL("your remote url");
HttpURLConnection conn = (HttpURLConnection) u.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty( "Content-Type", type );
conn.setRequestProperty( "Content-Length",
String.valueOf(encodedData.length()));
OutputStream os = conn.getOutputStream();
os.write(encodedData.getBytes());
There are a couple of thing going on here, Like URLEncoding is really mattered when came to security.
Note: Source of above code:here.
This is very Simple By using Java Clients you can Use RestTemplate or UniRest
That one running on Remote is simply Producer and the one which is in local is Consumer So you can exchange method of Resttemplate or get method of Unirest
Example Code is here.
#RequestMapping(value = "/testclient")
public String testclient()
{
HttpHeaders headers = new HttpHeaders();
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
HttpEntity<String> entity = new HttpEntity<String>(headers);
return restTemplate.exchange("https://www.mocky.io/v2/5185415ba171ea3a00704eed", HttpMethod.GET, entity, String.class).getBody();
}
For Unirest code is like this
HttpResponse<JsonNode> jsonResponse = null;
try {
jsonResponse = Unirest.get("https://www.mocky.io/v2/5185415ba171ea3a00704eed")
.header("accept", "application/json").queryString("apiKey", "123").asJson();
} catch (UnirestException e) { // TODO Auto-generated catch block
e.printStackTrace();
}
return jsonResponse.getBody().toString();

How to pass array of variables to REST URL in android?

I have to make registration using REST URL. REST services are written in Java now i have to pass the set of parameters in that secGameIds parameter is like this [100,102]. Example registration using Insomnia:::
{
"firstName":"parent111",
"lastName":"sadfsdf",
"email":"abc#bbc.com",
"date":"2000-06-09",
"phoneNum":"8765654454",
"gender":"male",
**"secGameIds":[0,0],**
"roleId":102
}
How should i provide secGameIds parameter value is it a ArrayList or Array?
for remaining values i have created JSONObject class object and adding values to that object and 'm appending that object to url
{
JSONObject json = new JSONObject();
json.put("fistName","aaa");
..
..
HttpPost post = new HttpPost(uri);
post.setHeader("Content-type", "application/json");
post.setEntity(new StringEntity(json.toString(), "UTF-8"));
DefaultHttpClient client = new DefaultHttpClient();
httpresponse = client.execute(post);
}
where as for secGameId i have tried like below,
{
int[] secGameId = {100,102};
}
-- gives me an error in back-end like "nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of int[] out of VALUE_NUMBER_INT token"
I even tried by using
{
ArrayList<Integer> secGameId = new ArrayList<String>();
secGameId.add(100);
secGameId.add(102);
}
and passing to value...
{
json.put("secGameIds":secGameId)
}
again at server side i kicked with the same error.
Can anyone help me?
public static String httpPost(HashMap<String, String> map, String url,String token) {
Log.e("call ", "running");
HttpRequest request;
if(token!=null){
request = HttpRequest.post(url).accept("application/json")
.header("Authorization", "Token " + AppInfo.token).form(map);
}
else
request = HttpRequest.post(url).accept("application/json").form(map);
int responseCode = request.code();
String text = request.body();
Log.e("response", " "+responseCode+ " "+ text);
if(responseCode==400){
return "invalid_tocken";
}
else if(responseCode<200 || responseCode>=300) {
return "error";
}
return text;
}
Hope you can convert the JSONArray to HashMap. If you instead need to post it as a JSONArray itself, then OkHttp library will help you.

sending empty content with HttpClient

I am trying to connect to an API of another company.
from the doc there is ::
even with your GET request, you'll need to include the Java equivalent of
curl_setopt($ch, CURLOPT_POSTFIELDS, $content), and you can set $data equal
to an empty array.
$content in their example is an empty JSON array.
I am using org.apache.commons.httpclient.
i am not sure how to add post fields to a org.apache.commons.httpclient.methods.GetMethod or if it is even possible.
i tried faking with a Content-Length of 2 but the GET times out (probably looking for content that i am not providing. if i remove the content-length i get an invalid response from the api server)
HttpClient client = new HttpClient();
GetMethod method = new GetMethod("https://api.xxx.com/account/");
method.addRequestHeader("Content-Type", "application/json");
method.addRequestHeader("X-Public-Key", APKey);
method.addRequestHeader("X-Signed-Request-Hash", "xxx");
method.addRequestHeader("Content-Length", "2");
int statusCode = client.executeMethod(method);
I don't think GetMethod includes any means of attaching a request body, because a GET request isn't supposed to have a body. (But having a body isn't actually prohibited, either - see: HTTP GET with request body .)
You're trying to use documentation written with a different language and a different client library in mind, so you'll have to use trial and error a bit. It sounds like they expect a request with no body, and you already have that. There's no good reason why they'd require a "Content-Length" with GET, but if that's the case, try setting it to 0.
This is how i resolved this issue
Created this class
public class HttpGetWithEntity extends HttpEntityEnclosingRequestBase {
public HttpGetWithEntity() {
super();
}
public HttpGetWithEntity(URI uri) {
super();
setURI(uri);
}
public HttpGetWithEntity(String uri) {
super();
setURI(URI.create(uri));
}
#Override
public String getMethod() {
return HttpGet.METHOD_NAME;
}
}
Then the calling function looks like
public JSONObject get(JSONObject payload, String URL) throws Exception {
JSONArray jsonArray = new JSONArray();
CloseableHttpClient client = HttpClientBuilder.create().build();
HttpGetWithEntity myGet = new HttpGetWithEntity(WeeblyAPIHost+URL);
myGet.setEntity( new StringEntity("[]") );
myGet.setHeader("Content-Type", "application/json");
myGet.setHeader("X-Public-Key", APIKey);
HttpResponse response = client.execute(myGet);
JSONParser parser = new JSONParser();
Object obj = parser.parse( EntityUtils.toString(response.getEntity(), "UTF-8") ) ;
JSONObject jsonResponse = (JSONObject) obj;
return jsonResponse;
}

Junit test case for a restful client using mockito

I have no idea before how to write the test cases, when i saw online tutorials i understand how to write it for a simple method with success and failure scenario. Now i have a method for http get which calls a restful API and returns a json response. I have like 6 parameters to include in the url and get a json response back. Now, my understanding so far is for success scenario here i should just hard code those input parameters and test if i am getting json back and for failure not getting json response back. Is this correct or do i have to do something else?
i mean i have a code something like
public List getStoreLocations(StoreData storeData) {
List storeList = null;
try {
HttpClient httpclient = HttpClientBuilder.create().build();
StringBuilder urlStrngBuildr = new StringBuilder(
https://<hostname>/xyz/abc);
Utility.addParameterToUrl(urlStrngBuildr,
Utility.APP_NAME,
Constants.APP_VALUE);
Utility.addParameterToUrl(urlStrngBuildr,
Constants.VERSION_PARAM_NAME,
Constants.VERSION_PARAM_VALUE);
if (storeData.getCity() != null && storeData.getState() != null) {
StringBuilder addressParamValue = new StringBuilder(
storeData.getCity());
addressParamValue.append(Constants.COMMA);
addressParamValue.append(storeData.getState());
Utility.addParameterToUrl(urlStrngBuildr,
Constants.ADDRESS_PARAM_NAME,
addressParamValue.toString());
} else if (storeData.getZip() != null) {
Utility.addParameterToUrl(urlStrngBuildr,
Constants.ZIP_PARAM_NAME, storeData.getZip());
}
Utility.addParameterToUrl(urlStrngBuildr,
Constants.PRODUCT_PARAM_NAME,
storeData.getProduct());
Utility.addParameterToUrl(urlStrngBuildr,
Constants.COUNTRY_PARAM_NAME,
storeData.getCountry());
Utility.addParameterToUrl(urlStrngBuildr,
Constants.DISTANCE_PARAM_NAME,
storeData.getDistance());
Utility.addParameterToUrl(urlStrngBuildr,
Constants.SIZE_PARAM_NAME, storeData.getSize());
HttpGet getRequest = new HttpGet(new java.net.URI(
urlStrngBuildr.toString()));
getRequest.addHeader(BasicScheme.authenticate(
new UsernamePasswordCredentials(username,password),
Constants.ENCODING_TYPE, false));
JSONResponseHandler responseHandler = new JSONResponseHandler();
String json = httpclient.execute(getRequest, responseHandler)
.toString();
Gson gson = new Gson();
StoreResponse response = gson.fromJson(json,
StoreResponse.class);
StoreDetails[] strDetails = response.getResult();
storeDetailsList = Arrays.asList(strDetails);
} catch (Exception exeption) {
exeption.printStackTrace();
}
return storeList;
}
Maybe you should take a look at REST-assured, which is a REST API testing framework.
The nice thing is, that it is much easier to read, supports JSON and XML and allows you to test things like HTTP-Codes or specific values from the response.
get("/lotto")
.then()
.assertThat().body("lotto.lottoId", equalTo(5));
You could add your parameters with the param method:
given()
.param("key1", "value1")
.param("key2", "value2")
when().
aso...
If you need authentication, like in your code, you can just use something like the following:
given()
.auth()
.basic(username,password)
.when()
.get("/secured")
.then()
.statusCode(200);`
Hope this helps with your testing.
It looks like the main thign you need to mock on that method is the HTtpClient. So how about you create a method for getting the client, then mock that method so that it returns a mock HttpClient.
public HttpClient getHttpClient(){
return HttpClientBuilder.create().build();
}
Then in your method you will do:
HttpClient httpclient = getHttpClient();
Then in your unit test code you will mock the getHttpClient method like so..
HttpClient mockClient = mock(HttpClient.class);
MyClassBeingTested instance = spy(new MyClassBeingTested ());
when(instance .getHttpClient()).thenReturn(mockClient);
when(mockClient.execute(any(HttpGet.class),any(JSONResponseHandler.class)).thenReturn(testJsonString);
List actual = instance.getStoreLocations(storeData);
Something like that.

Calling a #POST from Jersey

I was able to get a #GET request working on jersey and the relevant code is as follows
The code for the server
#Path("/Text")
#GET
public String Hello() {
System.out.println("Text Being print");
return "Abc";
}
#POST
#Path("/post/{name}/{gender}")
public Response createDataInJSON(#PathParam("name") String data, #PathParam("gender") String data2) {
System.out.println("Post Method 1");
JSONObject obj = new JSONObject();
obj.put("Name", data);
obj.put("Gender", data2);
return Response
.status(200)
.entity(obj.toJSONString())
.build();
}
The #POST also works when the parameters are passed in the url. (as mentioned in the above code segment)
But, it doesn't work when the parameters are not sent via the url. Like the code which follows.
#POST
#Path("/post2")
public Response createDataInJSON2(#FormParam("action") String data) {
System.out.println("Post Method 2 : Data received:" + data);
JSONObject obj = new JSONObject();
obj.put("data", data);
return Response
.status(200)
.entity(obj.toJSONString())
.build();
}
May be the problem lies with the way the services are called.
//GET call (Plain Text)
System.out.println(service.path("Hello").accept(MediaType.TEXT_PLAIN).get(String.class));
//POST call (Param)
ClientResponse response = service.path("Hello/post/Dave/Male").post(ClientResponse.class);
System.out.println(response.getEntity(String.class));
//POST call (JSON)
String input = "hello";
ClientResponse response2 = service.path("Hello/post2").post(ClientResponse.class, input);
System.out.println(response2.getEntity(String.class));
Can anyone tell me what am I missing here?
Try to add a #Consumes (application/x-www-form-urlencoded) on your #POST method createDataInJSON2 and explicitly add the same mime type in your request service.path("Hello/post2").type(MediaType.APPLICATION_FORM_URLENCODED).post(ClientResponse.class, input).
Also consider that your input is just a simple string. Have a look at the class MultivaluedMap
If you have troubles with the Encoding then have a look at this post https://stackoverflow.com/a/18005711/3183976
Try this. This worked for me.
The post method:
#POST
#Path("/post2")
public Response post2(String data) {
System.out.println("Post method with File: " + data);
return Response
.status(200)
.entity(data)
.build();
}
The calling method:
ClientResponse response2 =service.path("Hello/post2").post(ClientResponse.class,"some value");
System.out.println(response2.getEntity(String.class));
Hope this helps. Peace.

Categories

Resources