ds15812330851 2018-06-15 15:12
浏览 82
已采纳

如何使用URL操作在golang中代理GET请求

I want to build a golang service which will listen for GET request, do some URL manipulation and then proxy a new request (to the manipulated URL) back to the browser:

(from browser -> server) GET http://www.example.com/7fbsjfhfh93hdkwhfbf398fhkef93..

(server manipulates URL - decrypts "7fbsjfhfh93hdkwhfbf398fhkef93.." -> "my-super-resource")

(server -> URL resource) GET http://www.somewhereelse.com/my-super-resource

(server -> browser) Response from http://www.somewhereelse.com/my-super-resource passed on to browser (using cors)

The whole chain will need to be synchronous which is ok. Is there a decent proxy library which allows for this sort of thing?

  • 写回答

1条回答 默认 最新

  • dsuw85815 2018-06-15 15:36
    关注

    You can do something like this in less than 10 lines of code with the Sling package:

    type Foo struct {
        Bar string `json:"bar"`
    }
    
    func Handler(w http.ResponseWriter, r *http.Request) {
        // Get the URL and decrypt it
        url := getUrl(r)
        decryptedUrl := decryptUrl(url)
    
        // Use the decrypted URL to make a request to another service
        var data *Foo
        req, err := sling.New().Get(decryptedUrl).Receive(data)
        if err != nil {
             // Handle error...
        }
    
        // Respond to the original request using the data from the other service
        respond(w, http.StatusOK, data)
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度