简体   繁体   English

如何从Postman到Java获取Body原始JSON数据

[英]how to get Body raw JSON data from Postman to Java

I have the variable customer-id and a value in the raw data in JSON format. 我在JSON格式的原始数据中有变量customer-id和一个值。 I want to get the value from postman to java. 我想从邮递员获取值到Java。 How can I implement it? 我该如何实施?

在此处输入图片说明

It is not a good idea to use Postman in Java. 在Java中使用Postman不是一个好主意。

But you can do the same thing using Java. 但是,您可以使用Java执行相同的操作。

First, you should call the same endpoint that Postman calls. 首先,您应该呼叫Postman呼叫的相同端点。 You can use Unirest to access this endpoint. 您可以使用Unirest访问此端点。

Unirest.post("http://api.callme.com/json")
    .asJson()

It is good to observe the METHOD that is called on Postman and use the same on Unirest. 最好观察在邮递员上调用的方法,并在Unirest上使用该方法。 In the code above, I'm using the method POST. 在上面的代码中,我正在使用POST方法。

Then, you will get a JSON object. 然后,您将获得一个JSON对象。

try this code THis is a basic code to get the data from json data and please search 试试这个代码这是从json数据中获取数据的基本代码,请搜索

Download this jar file json 下载此jar文件json

How to add jar file documentation here 如何在此处添加jar文件文档

HttpURLconnection HttpURLconnection

 String url = "http//url"; //define the url here
    URL obj; //making the object 
    obj = new URL(url);         
    HttpURLConnection con = (HttpURLConnection) obj.openConnection();
    con.setRequestMethod("GET");

//add authorisation if you have any //添加授权(如果有)

con.setRequestProperty("Content-Type", "application/json");
con.setRequestProperty("Accept-Language", "UTF-8");
int responseCode = con.getResponseCode();
System.out.println("\nSending 'GET' request to URL : " + url);
System.out.println("Response Code : " + responseCode);
JSONObject jsonObject = new JSONObject(response.toString());
String nid = jsonObject.get("customer-id").toString();

You should have to set content type to application/json . 您应该将内容类型设置为application / json And in your java web application side, you can fetch that json from request. 在Java Web应用程序端,您可以从请求中获取json。 If you are using servlet then you have to use request.getParameter("customer-id"); 如果使用的是servlet,则必须使用request.getParameter("customer-id");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM