如何将数据变量传递给单独的Java代码的RETS API调用

发布于 2025-01-25 02:54:53 字数 1307 浏览 0 评论 0原文

我有一个情况可以通过Java代码测试REST API的删除调用。我需要将带有2个变量(如下屏幕快照)的表单数据传递给API请求。有人请我路由我如何做。

”在此处输入图像描述”

try {                       
                URL url = new URL("http://localhost:8999/testsource");
                HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                conn.setRequestMethod("DELETE");
                conn.setRequestProperty("Accept", "*/*");
                conn.setRequestProperty("session", "Cii2vEBZDplu5fI9JNXiM5");

                if (conn.getResponseCode() != 200) {
                    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 (Exception e) {

                e.printStackTrace();

            }

I got a situation to test the REST API's Delete call through Java code. I need to pass Form Data with 2 variables as below screenshot to the api request. someone please route me how to do that..

enter image description here

try {                       
                URL url = new URL("http://localhost:8999/testsource");
                HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                conn.setRequestMethod("DELETE");
                conn.setRequestProperty("Accept", "*/*");
                conn.setRequestProperty("session", "Cii2vEBZDplu5fI9JNXiM5");

                if (conn.getResponseCode() != 200) {
                    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 (Exception e) {

                e.printStackTrace();

            }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凌乱心跳 2025-02-01 02:54:53

您的问题不太清楚,但是我会根据以下假设来回答它,即您的表单数据包含两个字段:

  • id

  • permanentDelete

     字符串data =“ id = the-id-goes-goes-here& permanentDelete = yes-or-no-goes-here”;
     byte [] bytestosend = data.getBytes(standardcharsets.utf_8);
     outputStream outputStream = conn.getOutputStream();
     outputStream.write(bytestosend);
     

Your question isn't very clear but I'll make an attempt to answer it based on the assumption that your form data contains two fields which are:

  • id

  • permanentDelete

     String data = "id=the-id-goes-here&permanentDelete=yes-or-no-goes-here";
     byte[] bytesToSend = data.getBytes(StandardCharsets.UTF_8);
     OutputStream outputStream = conn.getOutputStream();
     outputStream.write(bytesToSend);
    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文