Monday, June 28, 2021

Types of HTTP Clients

Postman:
        https://127.0.0.1:8443/internal/in/user/alert/6019/generic/push
        Headers:
        X-Caller-Id:IN_XX_QCO2
        X-Caller-Auth-Token:3hS4qU8rM3
        Content-Type:application/json
        X-User-Mobile:9916473300
        {
            "ticker": "Test PN",
            "title": "Money Received",
            "detail": "100 Received",
            "time": "20210628",
            "timeToLive" : 120
        }

OKHttp:
        OkHttpClient client = new OkHttpClient();
        MediaType mediaType = MediaType.parse("application/json");
        RequestBody body = RequestBody.create(mediaType, "{\n    \"ticker\": \"Test PN\",\n    \"title\": \"Money Received\",\n    \"detail\": \"100 Received\",\n    \"time\": \"20210628\",\n    \"timeToLive\" : 120\n}");
        Request request = new Request.Builder()
          .url("https://127.0.0.1:8443/internal/in/user/alert/6019/generic/push")
          .post(body)
          .addHeader("x-caller-id", "IN_MW_QCO2")
          .addHeader("x-caller-auth-token", "3hS4qU8rM3")
          .addHeader("content-type", "application/json")
          .addHeader("x-user-mobile", "9916473353")
          .addHeader("cache-control", "no-cache")
          .addHeader("postman-token", "1629e893-9d51-0433-22a9-31fc9ef8d408")
          .build();
        Response response = client.newCall(request).execute();


java.net.Http
        HttpRequest request = HttpRequest.newBuilder()
                .uri(URI.create("https://face-detection6.p.rapidapi.com/img/face-age-gender"))
                .header("content-type", "application/json")
                .header("x-rapidapi-host", "face-detection6.p.rapidapi.com")
                .method("POST", HttpRequest.BodyPublishers.ofString("{\r\n    \"url\": \"https://inferdo.com/img/face-3.jpg\",\r\n    \"accuracy_boost\": 3\r\n}"))
                .build();
        HttpResponse<String> response = HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofString());
        System.out.println(response.body());

AsyncHttp
        AsyncHttpClient client = new DefaultAsyncHttpClient();
        client.prepare("POST", "https://face-detection6.p.rapidapi.com/img/face-age-gender")
            .setHeader("content-type", "application/json")
            .setHeader("x-rapidapi-host", "face-detection6.p.rapidapi.com")
            .setBody("{\r
            \"url\": \"https://inferdo.com/img/face-3.jpg\",\r
            \"accuracy_boost\": 3\r
        }")
            .execute()
            .toCompletableFuture()
            .thenAccept(System.out::println)
            .join();
        client.close();
    
NodeJS Request:
        var request = require("request");
        var options = { method: 'POST',
          url: 'https://127.0.0.1:8443/internal/in/user/alert/6019/generic/push',
          headers: 
           { 'postman-token': 'f3a011c2-63e5-746e-c9ee-136059cc0b7e',
             'cache-control': 'no-cache',
             'x-user-mobile': '9916473353',
             'content-type': 'application/json',
             'x-caller-auth-token': '3hS4qU8rM3',
             'x-caller-id': 'IN_MW_QCO2' },
          body: 
           { ticker: 'Test PN',
             title: 'Money Received',
             detail: '100 Received',
             time: '20210628',
             timeToLive: 120 },
          json: true };
        request(options, function (error, response, body) {
          if (error) throw new Error(error);
          console.log(body);
        });

NodeJS Native:
        var http = require("https");
        var options = {
          "method": "POST",
          "hostname": "127.0.0.1",
          "port": "8443",
          "path": "/internal/in/user/alert/6019/generic/push",
          "headers": {
            "x-caller-id": "IN_MW_QCO2",
            "x-caller-auth-token": "3hS4qU8rM3",
            "content-type": "application/json",
            "x-user-mobile": "9916473353",
            "cache-control": "no-cache",
            "postman-token": "657200ee-90d5-b612-856e-493477f98309"
          }
        };
        var req = http.request(options, function (res) {
          var chunks = [];
          res.on("data", function (chunk) {
            chunks.push(chunk);
          });
          res.on("end", function () {
            var body = Buffer.concat(chunks);
            console.log(body.toString());
          });
        });
        req.write(JSON.stringify({ ticker: 'Test PN',
          title: 'Money Received',
          detail: '100 Received',
          time: '20210628',
          timeToLive: 120 }));
        req.end();


Python http
        import http.client
        conn = http.client.HTTPSConnection("127.0.0.1:8443")
        payload = "{\n    \"ticker\": \"Test PN\",\n    \"title\": \"Money Received\",\n    \"detail\": \"100 Received\",\n    \"time\": \"20210628\",\n    \"timeToLive\" : 120\n}"
        headers = {
            'x-caller-id': "IN_MW_QCO2",
            'x-caller-auth-token': "3hS4qU8rM3",
            'content-type': "application/json",
            'x-user-mobile': "9916473353",
            'cache-control': "no-cache",
            'postman-token': "68851f89-d87e-7ccd-ee64-130e0cf519fe"
            }
        conn.request("POST", "/internal/in/user/alert/6019/generic/push", payload, headers)
        res = conn.getresponse()
        data = res.read()
        print(data.decode("utf-8"))


wget:
        wget --quiet \
          --method POST \
          --header 'x-caller-id: IN_MW_QCO2' \
          --header 'x-caller-auth-token: 3hS4qU8rM3' \
          --header 'content-type: application/json' \
          --header 'x-user-mobile: 9916473353' \
          --header 'cache-control: no-cache' \
          --header 'postman-token: 1650c649-5ae2-09ed-b6f7-39fef6bcef9c' \
          --body-data '{\n    "ticker": "Test PN",\n    "title": "Money Received",\n    "detail": "100 Received",\n    "time": "20210628",\n    "timeToLive" : 120\n}' \
          --output-document \
          - https://127.0.0.1:8443/internal/in/user/alert/6019/generic/push
  

curl 
curl is a command line tool to transfer data to or from a server, using any of the supported protocols (HTTP, FTP, IMAP, POP3, SCP, SFTP, SMTP, TFTP, TELNET, LDAP or FILE). 
curl is powered by Libcurl. This tool is preferred for automation, since it is designed to work without user interaction. curl can transfer multiple file at once.

	curl https://www.geeksforgeeks.org
	curl http://site.{one, two, three}.com      URLs with multiple sequence
	curl ftp://ftp.example.com/file[1-20].jpeg

curl displays a progress meter during use to indicate the transfer rate, amount of data transferred, time left etc.
	curl -# -o ftp://ftp.example.com/file.zip
	curl --silent ftp://ftp.example.com/file.zip

curl -o hello.zip ftp://speedtest.tele2.net/1MB.zip    -o : saves the downloaded file on the local machine with the name provided in the parameters.

curl -O ftp://speedtest.tele2.net/1MB.zip        -O : This option downloads the file and saves it with the same name as in the URL.

curl -C - -O ftp://speedtest.tele2.net/1MB.zip   -C – : This option resumes download which has been stopped due to some reason. This is useful when downloading large files and was interrupted.

curl --limit-rate 1000K -O ftp://speedtest.tele2.net/1MB.zip   
–limit-rate : This option limits the upper bound of the rate of data transfer and keeps it around the given value in bytes.

curl -u demo:password -O ftp://test.rebex.net/readme.txt   
-u : curl also provides options to download files from user authenticated FTP servers.

curl -u {username}:{password} -T {filename} {FTP_Location}  
-T : This option helps to upload a file to the FTP server. If you want to append a already existing FTP file you can use the -a or –append option.


curl https://www.geeksforgeeks.org > log.html --libcurl code.c   
–libcurl :This option is very useful from a developers perspective. If this option is appended to any cURL command, it outputs the C source code 
that uses libcurl for the specified option. It is the code similar to the command line implementation. The above example downloads the HTML and 
saves it into log.html and the code in code.c file. 

curl -u [user]:[password] -x [proxy_name]:[port] [URL...]        -x, –proxy : curl also lets us use a proxy to access the URL.

Sending mail : As curl can transfer data over different protocols, including SMTP, we can use curl to send mails.
curl –url [SMTP URL] –mail-from [sender_mail] –mail-rcpt [receiver_mail] -n –ssl-reqd -u {email}:{password} -T [Mail text file]


curl --request POST \
          --url https://127.0.0.1:8443/internal/in/user/alert/6019/generic/push \
          --header 'cache-control: no-cache' \
          --header 'content-type: application/json' \
          --header 'postman-token: ee5f1533-7177-a53c-5710-e209a4964d8e' \
          --header 'x-caller-auth-token: 3hS4qU8rM3' \
          --header 'x-caller-id: IN_MW_QCO2' \
          --header 'x-user-mobile: 9916473353' \
          --data '{\n    "ticker": "Test PN",\n    "title": "Money Received",\n    "detail": "100 Received",\n    "time": "20210628",\n    "timeToLive" : 120\n}'

curl -X POST \
          https://127.0.0.1:8443/internal/in/user/alert/6019/generic/push \
          -H 'cache-control: no-cache' \
          -H 'content-type: application/json' \
          -H 'postman-token: 10066716-e451-e635-bc32-50582471623a' \
          -H 'x-caller-auth-token: 3hS4qU8rM3' \
          -H 'x-caller-id: IN_MW_QCO2' \
          -H 'x-user-mobile: 9916473353' \
          -d '{
            "ticker": "Test PN",
            "title": "Money Received",
            "detail": "100 Received",
            "time": "20210628",
            "timeToLive" : 120
        }'

Java Script: Ajax
        var settings = {
          "async": true,
          "crossDomain": true,
          "url": "https://127.0.0.1:8443/internal/in/user/alert/6019/generic/push",
          "method": "POST",
          "headers": {
            "x-caller-id": "IN_MW_QCO2",
            "x-caller-auth-token": "3hS4qU8rM3",
            "content-type": "application/json",
            "x-user-mobile": "9916473353",
            "cache-control": "no-cache",
            "postman-token": "9e0d19ed-6525-0fa3-e978-f34823c861a4"
          },
          "processData": false,
          "data": "{\n    \"ticker\": \"Test PN\",\n    \"title\": \"Money Received\",\n    \"detail\": \"100 Received\",\n    \"time\": \"20210628\",\n    \"timeToLive\" : 120\n}"
        }
        $.ajax(settings).done(function (response) {
          console.log(response);
        });

PHTP:
        <?php
        $request = new HttpRequest();
        $request->setUrl('https://127.0.0.1:8443/internal/in/user/alert/6019/generic/push');
        $request->setMethod(HTTP_METH_POST);
        $request->setHeaders(array(
          'postman-token' => '5c0297c0-6f96-3731-d7b6-a9bc9869ce98',
          'cache-control' => 'no-cache',
          'x-user-mobile' => '9916473353',
          'content-type' => 'application/json',
          'x-caller-auth-token' => '3hS4qU8rM3',
          'x-caller-id' => 'IN_MW_QCO2'
        ));
        $request->setBody('{
            "ticker": "Test PN",
            "title": "Money Received",
            "detail": "100 Received",
            "time": "20210628",
            "timeToLive" : 120
        }');
        try {
          $response = $request->send();

          echo $response->getBody();
        } catch (HttpException $ex) {
          echo $ex;
        }

No comments:

Post a Comment