How to view headers of default JDK HttpURLConnection? - java

There's getHeaderFields() but that returns the headers in the response HTTP message. I want the headers in the request message, not the response.
Also, what is the difference between Properties and Headers? It seems like setRequestProperty() is setting a header, but I'm not sure.

If you check the method getRequestProperties() you'll find your guess is right:
public Map<String,List<String>> getRequestProperties()
Returns an unmodifiable Map of general request properties for this
connection. The Map keys are Strings that represent the request-header
field names. Each Map value is a unmodifiable List of Strings that
represents the corresponding field values.

Related

Sending optional parameters as empty in a web-service requestbody which do not have values

I am trying to consume some rest webservices with json request and
response format. I am sending all the values in the request even the
optional parameters which do not have values.(The default value for
all the optional parameter I set as empty string "" to avoid null).
One of my colleague pointed out to check empty parameters for optional
values and remove them from the request if no value exist.
I know I would be stupid to ask this very basic question but I would like to know the best practice:
Is it good to send empty parameters or we need to check for empty
parameters and send only the one which has value.
Generally the best practise is to follow the API.
But if removing optional parameters which do not have values does not affect the target consumers of the API, then you can go for it.
You can check the answer to following post, to get some good reasons to keep the null fields.
Is it Worth To exclude null Fields.

rest-assured jsonPath returns HashMap instead of LinkedHashMap

This pertains to Java.
I would like to be able to change the default behavior of how a JsonPath object returns itself when parsed from a json response so that I can still leverage the methods it comes with such as getMap(), getList(), etc. Ideally I would like all the JsonPath methods to return their Map objects as a LinkedHashMap instead of a HashMap or at the minimum have the getMap() method return as a LinkedHashMap so I can preserve key ordering.
The below response object's json key ordering matches the browser's json response:
Response response = given().get(urlQuery).then().extract().response();
However, when you attempt to grab an object or value from the response via jsonPath() then the json key ordering is all screwed up due to the fact that JsonPath methods are leveraging a HashMap instead of a LinkedHashMap behind the scenes such as the code snippet below does:
Map map = response.jsonPath().getMap("path.to.a.map");
I'm hoping the answer lies within changing the config to something, or overloading a method somewhere, etc. as I like using the rest-assured library for all my json parsing except I now need to preserve key ordering.
Aside from the response object as mentioned above, I would be content if I could at the minimum get the JsonPath methods to return the json in the correct order as shown by the below code example:
import com.jayway.restassured.path.json.JsonPath;
String json = "{\"fields\":{\"field1\":1,\"field2\":2,\"field3\":3,\"field4\":\"4\"}}";
// The value of the JsonPath object stays in the correct order: {"fields":{"field1":1,"field2":2,"field3":3,"field4":"4"}}
JsonPath jsonpath = new JsonPath(json);
// When using any of the JsonPath methods the order is messed up and returns: "{field4=4, field3=3, field2=2, field1=1}"
Object map = jsonpath.getMap("fields");
I would like to somehow get the JsonPath methods to keep the order by leveraging LinkedHashMap types but am unsure how or where to implement this.

Differance between getHeaderFields v/s getRequestProperties in HttpURLConnection in android

I was going through the HttpURLConnection API doc and found two methods
getHeaderFields () Which Returns an un-modifiable map of the response-header fields and values
and
getRequestProperties () which also Returns an un-modifiable map of general request properties used by this connection.
I want to know what is difference between this two methods?
From My understanding both methods are used to store key,value pair in HTTP Header.
Correct me if I am wrong.
Thanks in advance.
getHeaderFields() returns all the fields and values while getRequestProperties(String field) returns the properties of the requested fields (not all).
From < Android API 22 Platform > javaDoc:
public Map<...> getHeaderFields ()
Added in API level 1
Returns an unmodifiable map of the response-header fields and values. The response-header field names are the key values of the map. The map values are lists of header field values associated with a particular key name.
Some implementations (notably HttpURLConnection) include a mapping for the null key; in HTTP's case, this maps to the HTTP status line and is treated as being at position 0 when indexing into the header fields.
Returns
the response-header representing generic map
And for request
public Map<...> getRequestProperties ()
Added in API level 1
Returns an unmodifiable map of general request properties used by this connection. The request property names are the key values of the map. The map values are lists of property values of the corresponding key name.
Returns
the request-property representing generic map.

Convert HTML form Post Param string to Java Map

I am reading the request body payload from an HttpServletRequest instance. the body is created by HTML form post. e.g. "param1=value1&param2=value2&param1=value3", thus it will have string and array as the value of the param. And I want to convert it into Map so that the Object can be either String or List
Is there a utility method I can use from apache-commons, googlecommon/guava, or springframework that can make this magic happen without me checking whether it is an array of values or just a single value etc?

Does this return the pointer to the values or does it copy the values?

Well OK, I got confused. I believe it returns the pointer to the original map?
private HttpServletRequest originalRequest;
Map params = originalRequest.getParameterMap();
params.remove("parameter-to-remove");
params.put("parameter-to-add", "<a value>");
Now are the parameters in the originalRequest going to change after these actions? Or does it just copy the values to params and it doesn't matter what I do with them and nothings going to be changed in originalRequest?
Returned map is immutable Map, that could be the reason why you are not seeing the changes reflected.
As per getParameterMap javadoc
an immutable java.util.Map containing parameter names as keys and parameter values as map values. The keys in the parameter map are of type String. The values in the parameter map are of type String array.
If you would like to set some value to request, you should use setAttribute.
No, you are not allowed to remove or add any request parameter(s) to the request object. They must remain (as they arrived to the server) until the request object goes out of scope (after the end of the request processing cycle).
Logically, if you were allowed to do something like that, then the request object would not represent the original request any more. During the whole request processing cycle, we want to process the request sent by the client, not the one that has been tampered.
The method you should use instead is void setAttribute(java.lang.String name, java.lang.Object o).

Categories

Resources