Javacadabra
Javacadabra

Reputation: 5768

Sending POST Request headers to web server

I am trying to send a POST request to a php file located on a web server. Currently I am successfully sending the request but the headers are not being sent correctly.

Below is my query string I would like to send:

lastName=Jones&title=Android+Game+Programming+2&price=22.99&isbn=9876543210123&year=2012&firstName=Joe&publisher=Android+Press

and this is how I attempt to send them to the server:

if(method.equalsIgnoreCase("POST")){

        //Write the http post request to web server
        s.getOutputStream().write(("POST " + path + " HTTP/1.0\r\n").getBytes("ASCII"));
        s.getOutputStream().write("Host: www.jdiadt.com\r\n\r\n".getBytes("ASCII"));

        //Request Headers
        String title = "title: "+request.getParameters().get("title") + "\r\n";
        String firstName = "firstName: "+request.getParameters().get("firstName") + "\r\n";
        String lastName = "lastName: " + request.getParameters().get("lastName") + "\r\n";
        String isbn = "isbn: " + request.getParameters().get("isbn") + "\r\n";
        String publisher = "publisher: " + request.getParameters().get("publisher") + "\r\n";
        String year = "year: " + request.getParameters().get("year") + "\r\n";
        String price = "price: " + request.getParameters().get("price") + "\r\n";

       s.getOutputStream().write(title.getBytes("ASCII"));
       s.getOutputStream().write(firstName.getBytes("ASCII"));
       s.getOutputStream().write(lastName.getBytes("ASCII"));
       s.getOutputStream().write(isbn.getBytes("ASCII"));
       s.getOutputStream().write(publisher.getBytes("ASCII"));
       s.getOutputStream().write(year.getBytes("ASCII"));
       s.getOutputStream().write(price.getBytes("ASCII"));

         //Blank line
        String blankline = "\r\n";
        s.getOutputStream().write(blankline.getBytes("ASCII"));

        //Flush and wait for response...
        s.getOutputStream().flush();

When I run the code I get this notice from the script which leads me to believe I am not sending the headers correctly:

Notice: Undefined index: title in C:\wamp\www\bookstore\createBook.php on line 3

It gives above error for every single line where I try to retrieve the variables sent via POST. Here is code:

$title = $_POST['title'];
$firstName = $_POST['firstName'];
$lastName = $_POST['lastName'];
$publisher = $_POST['publisher'];
$isbn = $_POST['isbn'];
$year = $_POST['year'];
$price = $_POST['price'];

Any information as to what might be wrong?

createBook.php

<?php

$title = $_POST['title'];
$firstName = $_POST['firstName'];
$lastName = $_POST['lastName'];
$publisher = $_POST['publisher'];
$isbn = $_POST['isbn'];
$year = $_POST['year'];
$price = $_POST['price'];

try {
    require_once 'BookDAO.php';
    require_once 'Book.php';

    $dao = new BookDAO();
    $book = new Book(NULL, $title, $firstName, $lastName,
                    $publisher, $isbn, $year, $price);

    $dao->insert($book);

    $books = $dao->findAll();


    if (count($books) > 0) {
        echo '<table>';
        echo '<tr>';
        echo '  <th>Title</th>
                            <th>First name</th>
                            <th>Last name</th>
                            <th>Year</th>
                            <th>Price</th>
                            <th>Actions</th>';
        echo '</tr>';
        foreach ($books as $book) {
            echo '<tr>';
            echo '<td>' . $book->getTitle() . '</td>';
            echo '<td>' . $book->getFirstName() . '</td>';
            echo '<td>' . $book->getLastName() . '</td>';
            echo '<td>' . $book->getYear() . '</td>';
            echo '<td>' . $book->getPrice() . '</td>';
            echo '<td>';
            echo '<a href="editBookForm.php?id=' . $book->getId() . '">';
            echo '<img src="images/edit20.png" alt="Edit Book" />';
            echo '</a>';
            echo '<a href="deleteBook.php?id=' . $book->getId() . '"';
            echo ' onclick="return confirm(\'Are you sure you want to delete';
            echo ' this book?\');">';
            echo '<img src="images/delete20.png" alt="Delete Book" />';
            echo '</a>';
            echo '</td>';
            echo '</tr>';
        }
        echo '</table>';
    }
    else {
        echo "<p>There are no books in the database.</p>";
    }
    echo '<p>';
    echo '<a href="createBookForm.php">';
    echo '<img src="images/new20.png" alt="New Book" /> New Book';
    echo '</a>';
    echo '</p>';
}
catch (PDOException $e) {
    exit("Connection failed: " . $e->getMessage());
}
?>

Upvotes: 0

Views: 601

Answers (3)

Javacadabra
Javacadabra

Reputation: 5768

I fixed the problem myself. For anyone wondering what I did here is my solution.

As you can see in the above code I included with my original question I was trying to send the headers one after the other which is the complete wrong thing to do!

I checked out the following two links (the second one particularly) and found them very helpful in explaining the POST request structure.

http://net.tutsplus.com/tutorials/other/http-headers-for-dummies/

http://ruturajv.wordpress.com/2005/12/25/http-post-request/

I then went back to my code and made the following changes to how I constructed my post request:

//POST REQUEST
    if(method.equalsIgnoreCase("POST")){

        //CONSTRUCT REQUEST
        String blankline = "\r\n";
        String query = request.getQueryString();
        String length = String.valueOf(query.length());
        System.out.println(length);

        //Write the http post request to web server
        s.getOutputStream().write(("POST " + path + " HTTP/1.0" +"\r\n").getBytes("ASCII"));
        s.getOutputStream().write(("Host: localhost.com" + "\r\n").getBytes("ASCII"));
        s.getOutputStream().write(("Content-Type: application/x-www-form-urlencoded"+"\r\n").getBytes("ASCII"));
        s.getOutputStream().write(("Content-Length: " + length + "\r\n").getBytes("ASCII"));
        s.getOutputStream().write(blankline.getBytes("ASCII"));
        s.getOutputStream().write(query.getBytes("ASCII"));

        //Flush and wait for response...
        s.getOutputStream().flush();

I then simply read the response back from the server.

Upvotes: 1

Kurt Du Bois
Kurt Du Bois

Reputation: 7665

You are setting your parameters in a GET-type (querystring) of way and are trying to get them using POST. You should change either of those so that they are the same.

Upvotes: 1

rtcarlson
rtcarlson

Reputation: 424

Why not use a library like HttpClient? It's got a really nice API for performing HTTP GET, POST and other methods. It will allow you to write code that's less fragile and more understandable.

Link: http://hc.apache.org/httpclient-3.x/

Upvotes: 1

Related Questions