Legacy Java applications used slack-api to notify me, but by early March 2020 the notification failed. To troubleshoot on my Mac, I wanted to see what the payload of a request that failed to notify was. I tried to take a trace using Fiddler, but it didn't come in, so [Charles](https://www.charlesproxy. To use com /). In curl, referring to here, if you specify the proxy to localhost: 8888 with -x, you can get the log with Chales. ..
.sh
curl -x localhost:8888 -X POST --data-urlencode "payload={\"channel\": \"#general\", \"username\": \"gpedro Bot\", \"text\": \"this is#Posted in general.\", \"icon_emoji\": \":ghost:\"}" https://hooks.slack.com/services/<YOUR>/<TOKEN>
For Java applications, I wasn't quite sure how to take Http traces with Charles, so I decided to look into it.
My application used this library. It's been left untouched for a long time, and it seems to be minor, so I think that the people who are reading this article are definitely not using this library. However, by implementing Proxy class or equivalent settings, some form I suspect that there is a way to send an HTTP request via a Proxy in any library. [^ 1] When using this library, instead of creating an instance that calls the API by default, Proxy class I was able to create an instance by specifying (/reflect/Proxy.html). When I instantiated this way, I was able to use Charles to take an HTTP trace of requests for this library. If you want to troubleshoot the library, I hope it will be helpful in some way.
Install by referring to this article. Trust the certificate Charles Proxy .... in your keychain.
In the keychain, right-click on the Charles Proxy ... certificate and select Export-> [Export Charles Proxy CA] to export the certificate. The name of the certificate contains a lot of escape characters, so change the name to something simple if necessary.
Import as per this article. In my case, I was using AWS Corretto 8, so I imported it below. The password was as in the article above.
.sh
sudo keytool -import -trustcacerts -file ~/Documents/charles_proxy.cer -keystore /Library/Java/JavaVirtualMachines/amazon-corretto-8.jdk/Contents/Home/jre/lib/security/cacerts -alias ca
Make a request by specifying port 8888 for Charles, as you did with curl. In this library
import net.gpedro.integrations.slack.SlackApi;
import net.gpedro.integrations.slack.SlackMessage;
import java.net.InetSocketAddress;
import java.net.Proxy;
public class SlackExecute {
public static void main(String[] args) {
SlackMessage slackMessage = new SlackMessage(
"#genearal",
"gpedro Bot",
"Can I make a Slack request with gpedro?"
);
slackMessage.setIcon(":ghost:");
SlackApi api = new SlackApi(
"https://hooks.slack.com/services/<SOME>/<TOKEN>",
new Proxy(
Proxy.Type.HTTP,
new InetSocketAddress("localhost", 8888)
)
);
api.call(slackMessage);
}
}
Now, in Charles' left pane, https://hooks.slack.com will appear and you can trace the content.
.json
{
"channel": "#genearal",
"username": "gpedro Bot",
"icon_emoji": ":ghost:",
"unfurl_media": false,
"unfurl_links": false,
"link_names": false,
"text": "Can I request with gpedro"
}
[^ 1]: For example, make a request to Spring 5 [WebClient interface](https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/reactive/function/client/ WebClient.html) and specify the proxy in this article How to do it is described. For this class, [WebClient.Builder # clientConnector method](https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/reactive/function/client/WebClient.Builder. There is html # clientConnector-org.springframework.http.client.reactive.ClientHttpConnector-). The argument of this method can be ClientHttpConnector class .. [Constructor](https: /) of ReactorClientHttpConnector of this implementation class /docs.spring.io/spring/docs/current/javadoc-api/org/springframework/http/client/reactive/ReactorClientHttpConnector.html#ReactorClientHttpConnector-reactor.netty.http.client.HttpClient-) (https://projectreactor.io/docs/netty/release/api/reactor/netty/http/client/HttpClient.html) can be specified. TcpConfiguration method of HttpClient release / api / reactor / netty / http / client / HttpClient.html # tcpConfiguration-java.util.function.Function-) is the [TcpClient class](https://projectreactor.io/docs/netty/release/api/ TcpClient class with a function that takes reactor / netty / tcp / TcpClient.html) as an argument , Proxy method is ProxyProvider.TypeSpec takes a function as an argument. ProxyProvider.TypeSpec interface type method, Set the Proxy type to [ProxyProvider.TypeSpec interface type method](https://projectreactor.io/docs/netty/release/api/reactor/netty/tcp/ProxyProvider.TypeSpec.html#type-reactor. In the implementation of ProxyProvider.AddressSpec of the return value of netty.tcp.ProxyProvider.Proxy-) You can specify the host or the InetSocketAddress class (https://docs.oracle.com/javase/jp/8/docs/api/java/net/InetSocketAddress.html). It doesn't use the Proxy class (https://docs.oracle.com/javase/jp/8/docs/api/java/lang/reflect/Proxy.html), but it does the same thing.
Recommended Posts