form data to Login using Java API "Client-side coding should be in Java " with Body (user,pass)this is send as JSON to Server.Server sends back log-in Message in JSON format
JSON Data send by Client Looks like this
{
“usr":"@gmail.com”,
“pwd”:“mat”
}
I Need Client side Code in Java which might Look like this(some what).Please help.
public class NetClientPost {
public static void main(String[] args) {
try {
URL url = new URL(
"https://mat.erpnext.com/api/method/login");
Httpconn = (Http) url.Connection();
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.set("Content-Type", "application/json");
String input = "{\"usr\": \"mat@gmail.com\", \"pwd\": \"mat\"}";
os.write(input.getBytes());
os.flush();
if (conn.getResCo() != Http. {
throw new RuntimeException("Failed : HTTP error code : "
+ conn.getResponseCode());
}
BufferedReader br = new BufferedReader(new InputStreamReader(
(conn.getInputStream())));
String output;
System.out.println("Output from Server .... \n");
while ((output = br.readLine()) != null) {
System.out.println(output);
}
conn.disconnect();
} catch (MalformedException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
I am getting Error 401 . Why ?