Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: form-data - Body Request in java Post Request

  1. #1
    Junior Member
    Join Date
    Dec 2021
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post form-data - Body Request in java Post Request

    Hello Guys,

    Just want to know , how to implement the below postman collections in java. Basically how to implement the form-data as body in post method , could you please help me on this ?



    example:


    {
    "info": {
    "_postman_id": "48b6c4f9-eed9-4a9f-bfd6-5f4ed7220aea",
    "name": "Emailnotification",
    "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
    },
    "item": [
    {
    "name": "emailnotification_dev",
    "item": [
    {
    "name": "sendMail",
    "request": {
    "method": "POST",
    "header": [
    {
    "key": "X-IBM-Client-Id",
    "value": "",
    "type": "text"
    },
    {
    "key": "X-IBM-Client-Secret",
    "value": "",
    "type": "text"
    }
    ],
    "body": {
    "mode": "formdata",
    "formdata": [
    {
    "key": "body",
    "value": "{\n\"To\": \"MailID,MailID\",\n\"Subject\": \"TEST API V1\",\n\"Body\": \"<html> <body><h1>Test mail from API2</h1> </body> </html>\"\n}",
    "contentType": "",
    "description": "{\n\t\"To\": \"mailid\",\n\t\"Cc\": \"mailid\",\n\t\"Bcc\": \"mailid\",\n\t\"Subject\": \"IBMIntegrationBus\",\n\t\"Body\": \"<html> <body><h1> IBM App Connect </h1> </body> </html>\"\n}",
    "type": "text"
    },

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: form-data - Body Request in java Post Request

    This would be a big project if you are planning on writing the code yourself. Too big to get into here.
    The Apache project probably has some programs that would help you.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Dec 2021
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: form-data - Body Request in java Post Request

    can you please correct me here please

    String data = "{ \"To\": \"qqssabari@.yy.comssa\",\"Subject\": \"TEST API V12222\",\"Body\": \"<html> <body><h1>Test mail from API2</h1> </body> </html>\"}";

    try
    {
    System.setProperty("https.protocols", "TLSv1.2");

    URL url = new URL("https://test-api.email");
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setDoOutput(true);
    connection.setRequestMethod("POST");

    connection.setRequestProperty("X-IBM-Client-Id", "daab6");
    connection.setRequestProperty("X-IBM-Client-Secret", "dff");
    connection.setRequestProperty("Content-Type", "multipart/form-data" + "body="+data);
    DataOutputStream request = new DataOutputStream(connection.getOutputStream());
    System.out.println("sttaus" +connection.getResponseCode() );
    request.writeUTF("");

    request.flush();
    BufferedReader br = new BufferedReader(new InputStreamReader(
    (connection.getInputStream())));

    String output;
    System.out.println("Output from Server .... \n");
    while ((output = br.readLine()) != null) {
    System.out.println(output);
    }

    connection.disconnect();

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: form-data - Body Request in java Post Request

    please correct me here
    What needs correcting?
    What happens when the code is compiled and executed?

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Dec 2021
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: form-data - Body Request in java Post Request

    Below i have attched code for to POST request with Body as form-data Key name is body and value is

    {"To": "jayaveera.sabari@bupa.com.sa","Subject":"TEST API V12222","Body": "<html> <body><h1>Test mail from API2</h1> </body> </html>"}

    Code is not working as expected , anyone could you please guide to fix this issue?

    Appreciated all your reponses!!!!!





     
    String data = "{ \"To\": \"qqssabari@.yy.comssa\",\"Subject\": \"TEST API V12222\",\"Body\": \"<html> <body><h1>Test mail from API2</h1> </body> </html>\"}";
     
    try
    {
    System.setProperty("https.protocols", "TLSv1.2");
     
    URL url = new URL("https://test-api.email");
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setDoOutput(true);
    connection.setRequestMethod("POST");
     
    connection.setRequestProperty("X-IBM-Client-Id", "daab6");
    connection.setRequestProperty("X-IBM-Client-Secret", "dff");
    connection.setRequestProperty("Content-Type", "multipart/form-data" + "body="+data);
    DataOutputStream request = new DataOutputStream(connection.getOutputStream());
    System.out.println("sttaus" +connection.getResponseCode() );
    request.writeUTF("");
     
    request.flush();
    BufferedReader br = new BufferedReader(new InputStreamReader(
    (connection.getInputStream())));
     
    String output;
    System.out.println("Output from Server .... \n");
    while ((output = br.readLine()) != null) {
    System.out.println(output);
    }
     
    connection.disconnect();

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: form-data - Body Request in java Post Request

    Code is not working as expected
    What is expected? Do you get any response?
    Are there any error messages? How are you testing it?
    Do you have a test server you can send the message to so you can see what a server receives?

    Why doesn't the posted code have any indentations?
    Where is the catch for the try statement?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. how to convert this my PHP code 'send post request' to java
    By mr.msds in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 9th, 2013, 05:03 AM
  2. Can't form a simple POST or GET http request. No data returns.
    By goodguy in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 29th, 2011, 05:04 AM
  3. Problem sending POST request with Java..
    By lost in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 20th, 2010, 09:16 PM
  4. Replies: 4
    Last Post: August 13th, 2009, 05:54 AM