openresty request external interface with http or https

  Seves

原文链接:openresty request external interface with http or https

This article introduce to how to request external interface with http or https in openresty,it include ssl certified and don't ssl certified.

openresty request external interface with http:

  local http = require "resty.http"
  local httpc = http.new()
  local res, err = httpc:request_uri("http://example.com/helloworld", {
    method = "POST",
    body = "a=1&b=2",
    headers = {
      ["Content-Type"] = "application/x-www-form-urlencoded",
    }
  })

openresty request external interface with https,don't need ssl certified:

  local http = require "resty.http"
  local httpc = http.new()
  local res, err = httpc:request_uri("https://example.com:443/helloworld", {
    method = "POST",
    body = "a=1&b=2",
    headers = {
      ["Content-Type"] = "application/x-www-form-urlencoded",
    },
    ssl_verify = true(or false)
  })

openresty request external interface with https,need ssl certified:

if need ssl certified,it also need upstream and subrequest combined to request external interface:

  location /my_upstream/ {
      internal; # Specifies that a given location can only be used for internal requests
      proxy_pass_request_headers off;
      proxy_set_header Host backend.example.com;
      proxy_set_header Content-Type "application/json";
      proxy_set_header Accept "application/json";
      proxy_set_header Authorization "Bearer $bearerToken"
      proxy_pass                https://backend.example.com/; # trailing slash
      proxy_ssl_certificate     /etc/nginx/client.pem;
      proxy_ssl_certificate_key /etc/nginx/client.key
      proxy_ssl_verify on;
      proxy_ssl_verify_depth 2; #just example
      proxy_ssl_trusted_certificate /etc/nginx/ca.pem;
  }
  
  local res = ngx.location.capture('/my_upstream/login',
  {
       method = ngx.HTTP_POST,
       body = "some text",
       vars = {
                   bearerToken = "12345"
              }
  })


本文链接:https://www.tech-field.org/openresty-request-external-interface-with-http-or-https

fc