I am confused about something in python to send an HTTP request that I am trying to recreate in java

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

I am confused about something in python to send an HTTP request that I am trying to recreate in java

问题

以下是翻译好的部分:

朋友最近给我发了这段代码:

requests.post("example.com", headers = {'authorization': token}, json = {'content' : message})

这是用Python写的。它的目的是向example.com发送一个HTTP POST请求,我正在尝试将其转换为Java。

到目前为止,我有了这部分代码:

HttpURLConnection connection = (HttpURLConnection)url.openConnection();
//url在代码中稍前的位置有定义。
connection.setRequestMethod("POST");
connection.setDoOutput(true);
connection.setRequestProperty("Content-Type", "application/json");
connection.setRequestProperty("Authorization", token);

然而,在我朋友的代码中,他有两个不同的部分,分别是 headers={}json={}

我不知道在Java中应该如何处理这些。我不清楚headers和json之间的区别。我该如何选择消息的内容?请告诉我。

英文:

My friend recently sent me this code:

requests.post("example.com", headers = {'authorization': token}, json = {'content' : message})

it is in python. It is meant to send an HTTP post request to example.com and I am trying to convert it to java.

So far, I have this:

HttpURLConnection connection = (HttpURLConnection)url.openConnection();
//url is defined farther up in the code.
connection.setRequestMethod("POST");
connection.setDoOutput(true);
connection.setRequestProperty("Content-Type", "application/json");
connection.setRequestProperty("Authorization", token);

However, in my friends code he had 2 different parts that were headers={} and json={}

I don't know how I would do this in java. I am confused what the difference between headers and json is. How would I choose the content of the message? please let me know.

答案1

得分: 1

根据我理解,您在将 JSON 内容添加到请求时遇到了问题?

在 Java 中使用 setRequestProperty(),在 Python 中可以通过以下方式实现相同效果:

headers = {}
headers['Content-Type'] = "application/json"
headers['Authorization'] = token

我很久没用 Java 写过代码了,但根据我所了解,您需要执行以下步骤:

HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/json; utf-8");
connection.setRequestProperty("Authorization", token);
connection.setDoOutput(true);

// 以下部分将带有上述定义的标头的 JSON 数据发送到输出流中
String inputString = "{\"variable1\": \"myvariable1\", \"variable2\": \"myvariable2\"}";
try (OutputStream stream = connection.getOutputStream()) {
    byte[] input = inputString.getBytes("utf-8");
    stream.write(input, 0, input.length);
}

您已经通过 setRequestProperty() 方法设置了标头,现在只需要同时发送数据即可。

英文:

So from what I understand you having issues adding the json content to the request?

setRequestProperty() in java is the same in python as doing the following:

headers = {}
headers['Content-Type'] = "application/json"
headers['Authorization'] = token

I haven't written in Java for a long time but from what I have read you need to do the following:

HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/json; utf-8");
connection.setRequestProperty("Authorization", token);
connection.setDoOutput(true);

// This part sends the json data to the output stream with the headers defined above sent with the data
String inputString = "{variable1: "myvariable1", variable2; "myvariable2"}";
try(OutputStream stream = con.getOutputStream()){
    byte[] input = inputString.getBytes("utf-8");
    stream.write(input, 0, input.length);
}

You are already setting the headers via "setRequestProperty()" you just need to send the data as well.

huangapple
  • 本文由 发表于 2020年10月3日 11:04:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/64180314.html
匿名

发表评论

匿名网友

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

确定