Edit on 21st Nov : I have tried in accessing the URL directly using browser, it gives me the same 404 error response. But while checking in POSTMAN - code , i could see the call goes as below. So can someone please let me know how to send the call in sameway? thanks
POST /api/test/PW/authentication/user HTTP/1.1
Host: www.hostname.com X-API-Key: saksj82kqjsak290d
cache-control: no-cache
Postman-Token: 2b7fl871-195a-4782-9000-6ce269242704
Content-Type: multipart/form-data;boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="credential"
user123
Content-Disposition: form-data; name="password"
pass123 ------WebKitFormBoundary7MA4YWxkTrZu0gW--
Original Message : I have an issue while fetching token from a https Rest APi using Java code. Request URL : https://www.hostname.com/api/test/PW/authentication/user Curl : curl -X POST --header 'Content-Type: application/x-www-form-urlencoded' --header 'Accept: application/json' --header 'ApiKey: saksj82kqjsak290d -d 'userid=user123&password=pass123' 'https://www.hostname.com/api/test/PW/authentication/user' Method : POST
Once i send Post reqyest i will get the Token in Header values in JSON format. But while pinging the server i am getting 404 rsponse from Java. But with POSTMAN tool, i am getting the response as 200 with proper token in response header data.
Note : i have changed the URL here to some dummy.
So far i have tried as below and not sure where i have gone wrong.
import java.net.*;
import java.io.*;
import java.util.*;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.CookieHandler;
import java.net.CookieManager;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.net.HttpuConnection;
import javax.net.ssl.HttpsuConnection;
import java.lang.annotation.Retention;
import java.net.MalformedURLException;
public class Tokenhttps {
public static void main(String[] args) throws MalformedURLException {
int code;
String httpsurl = "https://www.hostname.com/api/test/PW/authentication/user";
String KeytoPass = "saksj82kqjsak290d";
try{
URL httpurl = new URL(httpsurl);
HttpuConnection uConnect = (HttpuConnection) httpurl.openConnection();
//Set header
uConnect.setDoOutput(true);
uConnect.setRequestMethod("POST");
urlConnect.setRequestProperty("Content-type", "application/x-www-form-urlencoded");
uConnect.setRequestProperty("Accept", "application/json");
uConnect.setRequestProperty("ApiKey", KeytoPass);
//Set body
String bodydata ="{\"userid\":\"user123\",\"password\":\"pass123\"}";
//POST
DataOutputStream dos = new DataOutputStream (uConnect.getOutputStream ());
dos.write(bodydata.getBytes());
dos.flush();
// Read the response.
InputStreamReader isr = null;
code = uConnect.getResponseCode();
System.out.println("responseCode:" + code );
if (code == 200) {
System.out.println("Connection is Success" );
isr = new InputStreamReader(uConnect.getInputStream());
//Print Header Fields
System.out.println("HeaderValues :"+uConnect.getHeaderFields ());
}
else {
isr = new InputStreamReader(uConnect.getErrorStream());
}
}
catch(Exception errormsg){
System.out.println("Error: " +errormsg);
}
}
}
It has worked with below change :
String bodaydata= "credential=user123&password=pass123";