如何在Java中使用okhttp3进行PUT请求?

huangapple go评论57阅读模式
英文:

How to use PUT request with okhttp3 in Java?

问题

I'm very new to this so please be nice. 我对此非常陌生,所以请友善一点。

I am trying to make a PUT request to some endpoint in which I need to send that some boolean variable is true or false. 我试图发出一个PUT请求到某个终端,其中我需要发送一个布尔变量,其值为true或false。

I am using okhttp3 with Java 11. 我正在使用Java 11和okhttp3。

final Request request = new Request.Builder()
.url(someBaseURL + "/" + obj.getKey() + "/path")
.build();
execute(request);

This is my code so far and I need to call the endpoint with the following URL: 这是我的代码,我需要调用以下URL的终端:

"someBaseURL/obj.key/path?booleanVariable=true"

So my question is how do I make a PUT request that adds this booleanVariable with its value being true or false. 所以我的问题是,我如何发出一个PUT请求,将这个booleanVariable添加到其中,其值为true或false。

I tried a very stupid and simple way just adding the "?booleanVariable=true" to the .url() but then that request is just a GET and it doesn't work for me. 我尝试了一个非常愚蠢和简单的方法,只是将"?booleanVariable=true"添加到.url()中,但那个请求实际上只是一个GET请求,对我来说不起作用。

英文:

I'm very new to this so please be nice. I am trying to make a PUT request to some endpoint in which I need to send that some boolean variable is true or false. I am using okhttp3 with Java 11.

final Request request = new Request.Builder()
        .url(someBaseURL + "/" + obj.getKey() + "/path")
        .build();
    execute(request);

This is my code so far and I need to call the endpoint with the following URL:

> "someBaseURL/obj.key/path?booleanVariable=true"

So my question is how do I make a PUT request that adds this booleanVariable with its value being true or false. I tried a very stupid and simple way just adding the "?booleanVariable=true" to the .url() but then that request is just a GET and it doesn't work for me.

答案1

得分: 1

如果您要在请求体中发送数据,请尝试以下代码:

MediaType mediaType = MediaType.parse("application/json");
RequestBody body = RequestBody.create(mediaType, "{\"first_param\":\"xxxxxx\"}");
Request request = new Request.Builder()
  .url("http://localhost:8080/api-path?booleanVariable=true")
  .put(body)
  .addHeader("Content-Type", "application/json")
  .build();

Response response = client.newCall(request).execute();

或者,如果您只想在请求参数中发送数据,请尝试以下代码:

MediaType mediaType = MediaType.parse("application/json");
RequestBody body = RequestBody.create(mediaType, "{\n\n}");
Request request = new Request.Builder()
  .url("http://localhost:8080/api-path?booleanVariable=true")
  .put(body)
  .addHeader("Content-Type", "application/json")
  .build();

Response response = client.newCall(request).execute();
英文:

If you are sending data in body then you try this code:

MediaType mediaType = MediaType.parse("application/json");
RequestBody body = RequestBody.create(mediaType, "{\n\t\"first_param\":\"xxxxxx\"}");
Request request = new Request.Builder()
  .url("http://localhost:8080/api-path?booleanVariable=true")
  .put(body)
  .addHeader("Content-Type", "application/json")
  .build();

Response response = client.newCall(request).execute();

or if you only sending data in params then you should try below code

MediaType mediaType = MediaType.parse("application/json");
RequestBody body = RequestBody.create(mediaType, "{\n\t\n}");
Request request = new Request.Builder()
  .url("http://localhost:8080/api-path?booleanVariable=true")
  .put(body)
  .addHeader("Content-Type", "application/json")
  .build();

Response response = client.newCall(request).execute();

huangapple
  • 本文由 发表于 2020年7月31日 18:56:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/63190555.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定