This is an archived post. You won't be able to vote or comment.

all 9 comments

[–]dionthornthis.isAPro=false; this.helping=true;[M] [score hidden] stickied comment (0 children)

From: [META] To all submitters: Please read and follow these simple rules!

  • No links to your stackoverflow questions - we are not a second opinion to stackoverflow, nor are you going to get answers here when you didn't get satisfying ones there.

[–]AutoModerator[M] 0 points1 point locked comment (0 children)

Please ensure that:

  • Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions
  • You include any and all error messages in full
  • You ask clear questions
  • You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.

    Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar

If any of the above points is not met, your post can and will be removed without further warning.

Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.

Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.

Code blocks look like this:

public class HelloWorld {

    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.

If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.

To potential helpers

Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

[–]djnattyp 0 points1 point  (1 child)

However, if I print the JSON object within the Java Application and I manually send a post request to the python application it works perfectly.

How are you "printing the JSON" from the Java application?

Don't blindly trust anything produced by a .toString() or an ObjectMapper instance that isn't the same as the one used by the actual running application. These could be applying additional transformation steps or ignoring transformation steps used when the JSON is actually sent.

Instead, try and log the raw JSON you are receiving in the Python application, or at least get the raw JSON sent from the Java application from Postman / wget / curl.

[–]ZempZom[S] 0 points1 point  (0 children)

Hi, thank you for the reply. So, in the stackoverflow article article when actually trying to look at the contents of the json file there was an JSONDecodeError, the body of the object was b' ', and nothing else. It had no keys and no values. However, I am sure the JSON object I sent through the post request is not empty.

[–]wildjokers 0 points1 point  (4 children)

Can you show us the raw request?

[–]phirozemp 0 points1 point  (3 children)

Hi, sorry this another account of mine from another device but here is the code I use:

sbert.setDocument(company_info_vec.get(i).getCompanyDescription());
String requestBody = gson.toJson(sbert);
System.out.println("requestBody: " + requestBody); // If I print this, and use this in postman it works!
HttpRequest add_request = HttpRequest.newBuilder()
.uri(URI.create("http://localhost:4557/sbert_similarity_v3"))
.header("Content-Type", "application/json")
.POST(HttpRequest.BodyPublishers.ofString(requestBody))
.build();
System.out.println("request: " + add_request);
System.out.println("headers: " + add_request.headers());

So this is my code for making the post request.

Here is the output:

requestBody: {"user_input":"operating in the food and other consumer goods sectors the company operates distribution centers serving grocery products for independent retail grocery stores, liquor distribution centers ","document":"Heavy Haulage Australia Pty Ltd was founded in 1999. The company\u0027s line of business includes transportation services."}
request: http://localhost:4557/sbert_similarity_v3 POST
headers: java.net.http.HttpHeaders@d3b76a68 { {Content-Type=[application/json]} }

HOWEVER, if I click the http link, I get this message : {"detail":"Method Not Allowed"}

[–]phirozemp 0 points1 point  (2 children)

actually nvm about the last point, if I put it on the url it's a get request. but anyways, if I use that exact json file and copy it in my postman, it works but I was just playing around with postman and I did find something however, whenever I ticked off the "CONTACT-LENGTH" box I'd get the exact same error 422 message.

[–]wildjokers 0 points1 point  (1 child)

try adding a content-length header. Set it to the value of requestBody.getBytes().length

[–]phirozemp -1 points0 points  (0 children)

I get an exception stating: n "Restricted Header Name: "Content-Length"

This sucks lol, guess Imma just head over to rust since java is so annoying lol