Why is Sinatra web server giving a 500 error from this POST method? - ruby

I'm building a Ruby web app with Sinatra, and am using a form to pass parameters to a POST page, which should have rendered dynamic content based on those parameters.
However, when it came time to test the POST method and submitting the form, the following was displayed in the browser:
Internal Server Error
bad content body
When this happens, the error log shows:
Error: EOFError: bad content body
followed by a stacktrace of files I'm not working with and then:
"POST /list/AUser'sName" HTTP/1.1 500 294
Code for the form:
Note - This page is just used for redirecting to a different page, determining the correct form action= attribute.
<form action="/list/<%=name%>" method='post' enctype="multipart/form-data">
<input type='Submit'/>
</form>
POST method in the server.rb file:
post '/list/:name' do
"Hello World"
end

After removing the enctype= attribute, the form is now working as intended.
From what I've learned, the enctype attribute is only used when uploading a file that needs to be submitted with the form. I had it included here because of past issues on different forms where logic errors occurred when it was not included.
Thanks to all for the comments and suggestions.

Related

How to submit a page in Oracle Apex using curl

I have a login page in an Oracle Apex application that works fine with a normal web browser like chrome. However when i try to perform the same operation using CURL (command-line browser), a HTTP 404 error is returned:
Request:
curl -i -d "P9999_USERNAME=MOIZ&P9999_PASSWORD=xxxx" -X POST http://localhost:8080/apex/f?p=101:9999:0:
Response:
HTTP/1.1 404 Not found
Server: Oracle XML DB/Oracle Database
Content-Type: text/html
Transfer-Encoding: chunked
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<HTML><HEAD>
<TITLE>404 Not found</TITLE>
</HEAD><BODY><H1>Not found</H1>
The requested URL /apex/f was not found on this server</BODY></HTML>
Using a normal browser, there are two sever request: one GET and one POST. However when using curl i am just making a single POST request.
Is that difference the cause of problem?
Is it possible to POST apex page without calling GET?
If yes then whether this solution will also work for file uploads?
Based on the post Python web scraping with BS using correct url? I was able to get answer to my above questions. [although this post uses python's requests and BeautifulSoup libraries for demonstration purpose, I think with some shell/windows scripting it may also be achievable with curl too]
Is that difference the cause of problem?
For POST request in APEX, we need to first perform a GET request as APEX expects the following hidden items to be sent as a payload of POST request
p_flow_id # application id
p_flow_step_id # page id
p_instance # session id
p_page_submission_id # page submission id
p_request # request
p_md5_checksum # md5 checksum
p_page_checksum # page checksum
p_arg_names # list of arguments
In addition, you may also need to add your page specific input items. For example,
p_t01 # username
p_t02 # password
Is it possible to POST apex page without calling GET?
For reasons mentioned in above answer, GET request will be required before POST request can be performed.

ajax returns page source, not the message

All the answers I saw here or elsewhere on Google were with jquery. This is not jquery.
I send an ajax string to a php file.
The php, among other things, formulates a message string which I echo
back to the client.
The returned string is put up in the client as an alert.
The form is then reset.
The problem is that when I do this it puts up as much of the page source that the alert can handle. If I open developer tools to look at the return, it puts the message up correctly, not the page source. Here is the return snippet in my ajax:
ajaxRequest.onreadystatechange = function(){
if(ajaxRequest.readyState == 4){
alert(ajaxRequest.responseText);
document.getElementById("thisForm").reset();
}
}
The php file does a simple echo of a text string.
What is it about developer tools that makes this run correctly and why doesn't it print out the message in the alert when developer tools is not there?
When I run the backend php by itself, with or without developer tools, it displays the message properly.
Does anyone have any ideas?
More information: I tried to replace the alert and reset with a
display.innerHTML=ajaxRequest.responseText where display is a javascript object formed from getElementById("ajaxReturn") of a "div id="ajaxReturn". It didn't work. When I tried developer tools, it showed the network response text as being the page source.
I also added && this.status == 200 to the if statement. No change.
The problem is solved. I am not deleting this because it might help some other poster who runs into the same problem. I launched the AJAX with an onclick to a javascript function called ajaxFunction(). The html entity containing the onclick had an href="#" in it. Removing that href solved the problem.
I had the exact same issue and my cause was related to having an extra slash in my URL.
Lets say my URL was:
https://example.com/index.php
I had a wrong link as follows:
https://example.com/index.php/
On both instances my server loads the page,
But the Ajax shows the page source as response for:
https://example.com/index.php/
But works fine for:
https://example.com/index.php
The ajax is essentially posting to index.php/ajaxpage.php which then responds with whats on index.php instead of whats on ajaxpage.php

Unintuitive behaviour of Poltergeist's `page.status_code`

We have a feature:
#smoke #acceptance
Scenario: Home page is available
When I visit the home page url
Then I expect no error code
With the final line implemented as:
Then(/^I expect no error code$/) do
expect(page.status_code).to eq 200
end
This sometimes fails. When debugging we've found the following:
the page itself always responds with 200
however, one resource within the page sometimes responds with 204
(which causes the test to fail)
This suggests that page.status_code does not equal the status code of the actual URL requested, but could be set to the status code of any (or presumably the last?) of the resources requested by the page.
Is this the correct explanation, and is it the expected behaviour of page.status_code?
Notes:
I realise that 204 could be construed as success, but this is not the
focus of this issue
status_code returns the result of the last request that triggered the phantomjs onLoadStarted callback - this should be the page, or potentially ajax requests initiated by the page -- it should not be assets being loaded directly as resources on the page (img, javascripts, css, etc). If it is a dependent asset then phantomjs has an issue and should be reported with reproducible example on that project. What type of request is reporting with a 204 response? If it's an ajax request then it is as expected - if not then its a bug in phantomjs. Note: checking response codes when using Capybara really is an anti-pattern, and you should generally stick to testing visible changes on the page.

Jmeter: 302 Moved Temporarily

I was trying to issue a https request through jmeter and observed am getting below response.
<html><head><title>302 Moved Temporarily</title></head>
<body bgcolor="#FFFFFF">
<p>This document you requested has moved temporarily.</p>
And it seems the actual url redirecting to different url which is getting the Response Code 200 which is OK. [The first urls response code is 302]
Also I have given an assertion for the page being loaded, but still that assertion fails [when I saw the response data in HTML format, observed that the respective page is not loaded]
Any help in resolving this issue would be a great help.
Looking at: http://jmeter.apache.org/usermanual/component_reference.html#Response_Assertion
If you chose "Main sample and sub samples" it will include the responses from the redirects. So for example if you're searching for "string" it will also include the response from the redirected page.

How to change simple form post into an AJAX calls

I have a really simple webform;
<form name="logmeinsupport" action="https://secure.logmeinrescue.com/Customer/Code.aspx" method="post">
<span>Enter your 6-digit PIN code: </span><input type="text" name="Code" /><br />
<input type="submit" value="Connect to technician" />
</form>
On the logmeinrescue side, there will post the error code back into URL:
Like: http://tomtom-uk--tst2.tomtom.com/app/utils/support_login?LogMeInRescueResponse=PINCODE_INVALID
However, our server setup interprets it as a page in web server, therefore, i got 404 error:
404 Page Not Found
The page 'utils/support_login?LogMeInRescueResponse=PINCODE_INVALID.php' was not found.
It is not so easy for our infrastructure team to change it. I am wondering whether I can use Ajax call backs post function to get response. And i think the difficult is about cross server ajax call.
Any examples? Thanks
Cheers,
Qing
The server looks for the file 'utils/support_login?LogMeInRescueResponse=PINCODE_INVALID.php'.
I think you should change your request to:
utils/support_login.php?LogMeInRescueResponse=PINCODE_INVALID
In this way the server will look for support_login.php and if there is indeed a file with this name on your server you will not have this error again.

Resources