Snellikeri
Snellikeri

Reputation: 3

Response code is 400 while using HttpURLConnection to POST in java for REST API

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);
		
		
	}
	
}

Upvotes: 0

Views: 686

Answers (1)

Merch0
Merch0

Reputation: 602

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));

Upvotes: 1

Related Questions