0

I am a newbie to REST API's. I need to post to the website, but I get response code as 400 and content-type as text/plain

If I use Advanced REST Client Application of google, I get different results. The response code is 500 and the content-type is text/html.

Am I not ending the post data (query1) correctly? Is this the correct way of doing it? Do I need to use JAX-RS? Can someone please help? Appreciate it.

import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;

import org.testng.annotations.Test;

public class RestfullAPIHttpURLConnection {
	
	@Test
	public static void postS() throws Exception {
		
		URL url;
		HttpURLConnection connection = null;  
		
		String urlParameters  = "[email protected]&profileName=Tester0&password=test&roleId=1";
		
		String email = "[email protected]";
		String profileName = "Tester0";
		String password = "test";
		int roleId = 1;
		
		String query = String.format("email=%s&profileName=%s&password=%s&roleId=%s", 
		     (email), 
		     URLEncoder.encode(profileName),
		     URLEncoder.encode(password),
		     (roleId));
		String query1="?";
		query1 = query1.concat(query);
		System.out.println("query1: " +query1);
		
		String type = "application/json";
		
		url = new URL("http://......com");
		connection = (HttpURLConnection)url.openConnection();
		
		connection.setDoOutput(true);
		connection.setRequestMethod("POST");
		connection.setRequestProperty( "Content-Type", type );
		
		connection.setRequestProperty( "charset", "utf-8");
		connection.setUseCaches( false );
		
		// creates an output stream on the connection and opens an OutputStreamWriter on it:
				
			OutputStream output = connection.getOutputStream();
			Writer writer = new OutputStreamWriter(output, "UTF-8");
			
			// client's output is the server's input.
		    writer.write(URLEncoder.encode(query1, "UTF-8"));
		
		
		String contentType=connection.getContentType();
		int responseCode=connection.getResponseCode();
		int len = connection.getContentLength();
		String rmsg = connection.getResponseMessage();
		
		System.out.println("ContentType: " +contentType);
		System.out.println("ResponseCode: " +responseCode);
		System.out.println("Content length: " +len);
		
		System.out.println("URL " + connection.getURL());
		System.out.println("Response msg: " + rmsg);
		
		
	}
	
}

1 Answer 1

1

Use Jersey Client:

Here an example:

final WebTarget target = ClientBuilder.newClient().target("http://......com");

final WebTarget webTargetWithParams = target.queryParam("email", "[email protected]")
                                            .queryParam("profileName", "Tester0")
                                            .queryParam("password", "test")
                                            .queryParam("roleId", "1");

final Response response = webTargetWithParams.request().get();

System.out.println(response.readEntity(String.class));
Sign up to request clarification or add additional context in comments.

2 Comments

But, if the server respond 400 that means: you didn't send the right data to the server in the POST. In your code, I don't see what data are you sending.
Is jersey Client the only way to do this? This is the data I am sending to the server String query = String.format("email=%s&profileName=%s&password=%s&roleId=%s", (email), URLEncoder.encode(profileName), URLEncoder.encode(password), (roleId)); String query1="?"; query1 = query1.concat(query); OutputStream output = connection.getOutputStream(); Writer writer = new OutputStreamWriter(output, "UTF-8"); writer.write(URLEncoder.encode(query1, "UTF-8"));

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.