3

For an assignment I have to create a program that uses rest. This is the code I was given by the teacher to get us started for this assignment, so this code below should be right.

import java.io.*;
import java.net.InetSocketAddress;
import java.util.*;
import java.util.concurrent.Executors;

import com.sun.net.httpserver.*;

public class HttpServerDemo {
    public static void main(String[] args) throws IOException {
        InetSocketAddress addr = new InetSocketAddress(8080);
        HttpServer server = HttpServer.create(addr, 0);
        server.createContext( "/", new RootHandler());  
        server.createContext( "/foo/", new FooHandler());   
        server.setExecutor( Executors.newCachedThreadPool());
        server.start();
        System.out.println("Server is listening on port 8080" );
    }

    public static void printHeaders( HttpExchange exchange, PrintStream response) {
        Headers requestHeaders = exchange.getRequestHeaders();
        Set<String> keySet = requestHeaders.keySet();
        Iterator<String> iter = keySet.iterator();
        while( iter.hasNext()) {
            String key = iter.next();
            response.println( key + " = " + requestHeaders.get(key));
        }
    }
    public static void printBody( HttpExchange exchange, PrintStream response) throws IOException {
        BufferedReader body = new BufferedReader( new InputStreamReader( exchange.getRequestBody()));
        String bodyLine;
        while( (bodyLine = body.readLine()) != null) {
            response.println( bodyLine);
        }
    }
}

class RootHandler implements HttpHandler {
    public void handle( HttpExchange exchange) throws IOException {
        String requestMethod = exchange.getRequestMethod();

        Headers responseHeaders = exchange.getResponseHeaders();
        responseHeaders.set( "Content-Type", "text/plain");
        exchange.sendResponseHeaders( 200, 0);

        PrintStream response = new PrintStream( exchange.getResponseBody());
        response.println( "context: ROOT; method: " + requestMethod);
        response.println( "--- headers ---");
        HttpServerDemo.printHeaders( exchange, response);
        if( requestMethod.equalsIgnoreCase( "POST")) {
            response.println( "=== body ===");
            HttpServerDemo.printBody( exchange, response);
        }
        response.close();
    }   
}

class FooHandler implements HttpHandler {
    public void handle( HttpExchange exchange) throws IOException {
        String requestMethod = exchange.getRequestMethod();

        Headers responseHeaders = exchange.getResponseHeaders();
        responseHeaders.set( "Content-Type", "text/plain");
        exchange.sendResponseHeaders( 200, 0);

        PrintStream response = new PrintStream( exchange.getResponseBody());
        response.println( "context: FOO; method: " + requestMethod);
        HttpServerDemo.printHeaders( exchange, response);
        response.close();
    }   
}

Since the RootHandler class has an if statement checking for "POST" I will use that to test it. So when I use curl from a separate terminal to communicate with this program I enter:

curl –d "message=helloworld" http://localhost:8080/

and I get this in return:

curl: (6) Could not resolve host: –d; nodename nor servname provided, or not known
curl: (6) Could not resolve host: message=helloworld; nodename nor servname provided, or not known
context: ROOT; method: GET
--- headers ---
Host = [localhost:8080]
User-agent = [curl/7.21.4 (universal-apple-darwin11.0) libcurl/7.21.4 OpenSSL/0.9.8r zlib/1.2.5]
Accept = [*/*]

I feel like I am making my mistake when I am using curl from my terminal. By looking at the errors it isnt taking the "-d" option I am giving it and it is causing the program to read the request method as "GET" instead of "POST". Ive tried this for "DELETE" and "PUT" request methods and got the same result.

2 Answers 2

1

That's not a dash:

curl –d "message=helloworld" http://localhost:8080/ # Not a dash
curl -d "message=helloworld" http://localhost:8080/ # Is a dash

It should be clear that the code is completely irrelevant, since the error you're getting is from curl.

While code should be right, that doesn't mean it is. Don't trust it just because you got it from a teacher, book, website, etc. All sorts of things can go wrong, like cut-and-paste issues, which is likely what happened with your curl command as well.

Sign up to request clarification or add additional context in comments.

2 Comments

Wow of course my mistake would be something that simple. Thanks!
@user1513200 While I suspected it from the beginning, you can also use the process of elimination: curl localhost, curl localhost:8080, etc. until something breaks. If nothing breaks as you're typing it in bit-by-bit, suspect copy/paste. Try with different data. Sanity-check the options.
0
curl: (6) Could not resolve host: –d; nodename nor servname provided, or not known
curl: (6) Could not resolve host: message=helloworld; nodename nor servname provided, or not known

Those are curl errors, not caused by the remote host. After investigating your curl request, you use the wrong "-" character.

You are using –d when the real option is -d.

See how the size is different:

  • –d <- wrong
  • -d <- correct

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.