• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

HTTP POST from my java process throwing an error!

 
Ranch Hand
Posts: 389
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a java process which is listening to ActiveMQ messages and when the status is COMPLETE, I am calling HTTP POST as shown in the code below. I'm referring to the following
article (https://mkyong.com/java/how-to-send-http-request-getpost-in-java/) for sending POST. However, I'm running into following issue:

In the eclipse console, I am getting the following error:

 

When I used POSTMAN, the request worked fine but I had to make following changes in the POSTMAN (as shown in the screenshot below - encircled in red).



1) Put my parameters inside Body section of POSTMAN

2) Changed the type to JSON.

[![enter image description here][2]][2]


My relevant code below:



What am I doing wrong inside the `sendPost()` method while calling HTTP POST? Do I need to take care of JSON thing just like I did in POSTMAN. If yes, then how? Please advise. Thanks !

 
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your Java POST is saying it is sending the data as:
application/x-www-form-urlencoded

What Content-Type is you Postman POST sending out, according to the Header?
I assume application/json?

That's down to your use of UrlEncodedFormEntity.
That's not what a Json payload should use.  It should be just a StringEntity (and you'll need to set the header correctly as well).
 
Jack Tauson
Ranch Hand
Posts: 389
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dave Tolls wrote:Your Java POST is saying it is sending the data as:
application/x-www-form-urlencoded

What Content-Type is you Postman POST sending out, according to the Header?
I assume application/json?

That's down to your use of UrlEncodedFormEntity.
That's not what a Json payload should use.  It should be just a StringEntity (and you'll need to set the header correctly as well).



Yes, it's sending out application/json as I mentioned in the screenshot above (JSON encircled in red color).

1) So, how do I change it to JSON format then?

2) And I should replace UrlEncodecFormEntity with StringEntity? Could you explain with an example?

Thanks !

 
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The dropdown that Postman shows is not the content type of the request. It's just a hint how Postman displays your request in the text area. See the Headers tab for the real content type.

It's likely application/json though. I think Postman gives a warning when the content type doesn't match the display type.

So if the request from Postman works, that means the server expects application/json. You are not sending JSON from your client. You are sending an URL-encoded form.

You have a few options, from least to most preferable:

  • Use some JSON library to build a JSON string from your data, and then use StringEntity and set its content type to ContentType.APPLICATION_JSON.
  • See if Apache has some sort of built-in JsonEntity type.
  • Use a better HTTP client. Personally I favor javax.ws.rs.client.Client which will automatically convert your JAXB data types to JSON if you have a JSON message writer on the class path.
  •  
    Stephan van Hulst
    Saloon Keeper
    Posts: 15510
    363
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Here's an example of using JAX-RS. Don't let the XML annotations fool you, Entity.json() writes JAX-B data object as a JSON string.
     
    Jack Tauson
    Ranch Hand
    Posts: 389
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Jack Tauson wrote:

    Dave Tolls wrote:Your Java POST is saying it is sending the data as:
    application/x-www-form-urlencoded

    What Content-Type is you Postman POST sending out, according to the Header?
    I assume application/json?

    That's down to your use of UrlEncodedFormEntity.
    That's not what a Json payload should use.  It should be just a StringEntity (and you'll need to set the header correctly as well).



    Yes, it's sending out application/json as I mentioned in the screenshot above (JSON encircled in red color).

    1) So, how do I change it to JSON format then?

    2) And I should replace UrlEncodecFormEntity with StringEntity? Could you explain with an example?

    Thanks !




    So I modified my method like this and it seems to be working but for some reason, the record is not getting inserted into the database. Could anyone take a look what I might be doing wrong here?



    And here is the response I see in eclipse:

     
    Stephan van Hulst
    Saloon Keeper
    Posts: 15510
    363
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    You got a 401 response, meaning your client is not logged in.
     
    Jack Tauson
    Ranch Hand
    Posts: 389
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Stephan van Hulst wrote:You got a 401 response, meaning your client is not logged in.



    I see. You mean the URL which I'm calling isn't functional?
     
    Marshal
    Posts: 4501
    572
    VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Jack Tauson wrote:

    Stephan van Hulst wrote:You got a 401 response, meaning your client is not logged in.



    I see. You mean the URL which I'm calling isn't functional?


    No - a 401 response means that the resource/service that you are attempting to access requires authentication.
     
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    reply
      Bookmark Topic Watch Topic
    • New Topic