Handling form data for application/x-www-form-urlencoded contentType - forms

I have a textarea in a jsp page and the contents of that are processed in a Servlet.
I thought the contentType was multipart so I figured I'd use getFormField() and getString() methods to extract the values but this throws an exception saying
the request doesn't contain a multipart/form-data or multipart/mixed stream, content type header is application/x-www-form-urlencoded
When I use request.getParameter("textAreaID") it is always null. How do I handle this situation? Any help would be appreciated. Please let me know if the question is not framed in a proper manner before downvoting. Thanks in advance.

If u want to extract values of many fields,you can use getParameterNames() method
Enumeration paramNames = request.getParameterNames();
while(paramNames.hasMoreElements())
{
String parameter = (String) paramNames.nextElement();
String parameterValue = (String) request.getParameter(parameter);
System.out.println("*****" + parameter + " - " + parameterValue);
}

Related

Issue with parsing json response having xml attributes

I have a json response using the Http Client Adapter which has the below format
{
"?xml": {
"#version":"1.0",
"#encoding":"utf-8"
},
ArrayOfBusinessTypeAPI {
"#xmlns:xsd":"http://www.w3.org/2001/XMLSchema",
"#xmlns:xsi":"http://www.w3.org/2001/XMLSchema-instance",
...
}
}
I need some information on how the below elements can be defined in the event definition.
1.?xml
2.#version
3.#xmlns:xsd
As per the documentation "#" is used for substitution and : for coassignment.
Can someone please provide any insight as to how this can be done.
I assume you mean in the yaml mapper codec, in which case you can simply quote the keys and this should work:
mapFrom:
- payload.xml: "payload.?xml"
- payload.version: "payload.#version.myField2"
- payload.xmlns_xsd: "payload.#xmlns:xsd"
If you were asking how to declare an event in EPL to handle this response, you don't need to use the exact naming scheme in your event definition. You could have:
event Response {
string xml;
string version;
string xmlns_xsd;
}
And then using the mapper codec map between the two fields like so:
mapFrom:
- payload.xml: "payload.?xml"
- payload.version: "payload.#version.myField2"
- payload.xmlns_xsd: "payload.#xmlns:xsd"
EDIT
So the first thing stopping your event parsing is that your response isn't valid JSON. The line "ArrayOfBusinessTypeAPI {" would need to be ""ArrayOfBusinessTypeAPI": {". You can only use the mapper codec to parse JSON.
The second reason this isn't working is because the content type is set to text/html. Does the JSON codec have filterOnContentType set to true? If so, it won't transform this message.
However, if the JSON is valid and gets processed by the JSON codec, you can correctly map the event like so:
mapFrom:
- payload.id: metadata.requestId
- payload.xml: "payload.?xml"
- payload.version: "payload.xml.#version"
- payload.xsd: "payload.ArrayOfBusinessTypeAPI.#xmlns:xsd"
- payload.xsi: "payload.ArrayOfBusinessTypeAPI.#xmlns:xsi"
- payload.encoding: "payload.xml.#encoding"
Which maps to an event:
event Resp {
dictionary<string, string> xml;
string version;
string xsd;
}

How to pass response body field to other request's body (Gatling)

I have two end point.
-/authenticate
-/authenticate/verification
/authenticate return guid field on response body.
and /authenticate/verification requires that field on request body.
I have tried to get guid like this :
jsonPath("$..guid").saveAs("verificationGuid")
and pass it to other body :
.body(StringBody(s"{\"guid\":${verificationGuid}, \"code\":\"123456\"}"))
this is the code block:
def login = {
exec(http("Authenticate")
.post("/authenticate")
.body(StringBody(userString))
.headers(headerLogin)
.check(status is 200)
.check(jsonPath("$..guid").saveAs("verificationGuid"))
)
.exec(http( "Authenticate verify")
.post("/authenticate/verify")
.headers(headerLogin)
.body(StringBody(s"{\"guid\":${verificationGuid}, \"code\":\"123456\"}"))
.check(status is 200)
)
}
But it doesnt work, how can I do this?
Remove s from s"{\"guid\":${verificationGuid}, \"code\":\"123456\"}"). If s is in front of string every ${something} placeholder will be treated as Scala built in string interpolation and compiler will try to replace it with Scala variable, which in your case does not exist. Without s it will be treated as literal string and than caught by Gatling EL Parser and replaced with previously saved Gatling session attribute.

RestAssured response validation using body and array as parameter

I am trying to validate a REST response. Is it possible to use an array as parameter to containsonly?
Ex:
String values[] = line.split(",");
given().
when().
then().
statusCode(200).
body("value", containsOnly(values));
Also, can we use variables as parameters to other methods like HasItems, equalTo etc?
Ex: body(HasItems(values))
Yes, You could use any appropriate matcher to check whole body or just part of it. Just take attention on a object type returned by pecified path - first argument of body().
Try this :
Response resp = RestAssured.given()
.header("Content-Type", "application/vnd.dpd.public.v1+json")
.body(FixtureHelpers.fixture("request/request.json"))
.post("/");
resp
.then()
.statusCode(200)
.body("random.object", CoreMatchers.equalTo("value"));
This would work for request.json object like :
{"random":{"object": "value"}}

Sending String[] Array over J2ME Socket Connection using ObjectOuputStream

I am trying to send a String[] array in j2me using ObjectOUputStream, but i keep getting this error,
java.lang.IllegalArgumentException: Not an HTTP URL
Here is my code:
OutputStream os=null;
HttpConnection hc= null;
ObjectOutputStream oj=null;
//get the URL
String serverURL=entry.getUrl();
hc=(HttpConnection)Connector.open(serverURL, Connector.READ_WRITE, true);
hc=(HttpConnection)Connector.open(serverURL);
hc.setRequestMethod (HttpConnection.POST);
hc.setRequestProperty ("Content-Type", "application/x-www-form-urlencoded");
hc.setRequestProperty ("User-Agent", "Profile/MIDP-2.0 Configuration/CLDC-1.0");
hc.setRequestProperty ("Content-Language", "en-US");
System.out.println ("Posting to the URL: " + entry.getVectorParams());
//open the output stream to send the post parameters
//os=hc.openOutputStream();
oj=(ObjectOutputStream)hc.openOutputStream();
//writing post parameters
String[] bg=entry.getVectorParams();
oj.writeObject(bg);
Please give a suggestion.
I checked my URL, it is correct and regarding Connector.open(), i pasted it twice here, not in my actual code. Is there anything else that I am doing wrong?
The System.out.println("Posting to the URL: " + entry.getVectorParams()), this only prints the post parameters, I have the serverurl passed in here:
String serverURL=entry.getUrl();
hc=(HttpConnection)Connector.open(serverURL, Connector.READ_WRITE, true);
My server URL is : http://localhost:8080/Web/gServer.jsp
The value of your serverURL variable must not be a valid URL. Try printing it out, and checking.
You have this debug statement:
System.out.println ("Posting to the URL: " + entry.getVectorParams());
but that is printing out the params, not the url. You should print out the serverURL variable.
Also, you are calling Connector.open() twice in a row. There's no need for that.
Update: I also think there could be a problem with the way you're writing the POST parameters to your connection's OutputStream. I wouldn't use an ObjectOutputStream. See something like this for an example of making J2ME POST calls. Basically, you make a String of the POST parameters, separated by &, and then use String.getBytes() to convert to a byte[] for writing to the OutputStream.

How To URLEncode Facebook Post Data in C#

I am rewriting code from http://blog.blackballsoftware.com/2010/11/03/making-a-facebook-wall-post-using-the-new-graph-api-and-c/ to create a class to post to Facebook. The code works as long as I do not URLEncode the post data. For example: If the post data is "message=Test,please ignore" then it works. If I URLEncode the same data into "message%3dTest%2cplease+ignore" then I get the error {"error":{"message":"(#100) Missing message or attachment","type":"OAuthException","code":100}}.
Should the Post data be URLEncoded? I think it should because if I post a message like this, "Test&Message", then only the word Test appears.
Relevant code is below. If postParams = HttpUtility.UrlEncode(postParams); is commented out, then the code works. If not, Facebook returns the error that the message is missing.
postParams = HttpUtility.UrlEncode(postParams);
byte[] bytes = System.Text.Encoding.ASCII.GetBytes(postParams);
webRequest.ContentLength = bytes.Length;
System.IO.Stream os = webRequest.GetRequestStream();
os.Write(bytes, 0, bytes.Length);
os.Close();
try
{
var webResponse = webRequest.GetResponse();
}
catch (WebException ex)
{
StreamReader errorStream = null;
errorStream = new StreamReader(ex.Response.GetResponseStream());
error = errorStream.ReadToEnd() + postParams;
}
The answer can be found on Stackoverflow at C# Escape Plus Sign (+) in POST using HttpWebRequest. Use Uri.EscapeDataString and not URLEncode. Encode the parameter value only and not the equals sign after the parameter name. Example: message=Test%2Cplease%26%20ignore works but message%3dTest%2Cplease%26%20ignore does not work because the equals after the parameter name is encoded as %3d.