【问题标题】:How to translate curl command to urlfetch or request in general?通常如何将 curl 命令转换为 urlfetch 或请求?
【发布时间】:2018-05-08 11:38:02
【问题描述】:

我想从我的 gae 服务器向 api 发出 https 请求,例如使用 urlfetch。 示例调用以 curl 命令的形式给出。

curl <URL> \
  -u <USER_KEY>: \
  -d "infoa=123" \
  -d "infob='ABC" \
  -d "token=<SOME_TOKEN>" \
  -d "description=Test"

我只想知道 HTTPS 请求是什么样的,所以我可以使用 this 文档复制它。可能是错误的,但我已经使用了 --trace-ascii - 和 curl 但从输出中我仍然不能 100% 说出我发出的请求是什么样的。
他们将 http-request 的哪个方面翻译成?会不会有这样的工作:

result = urlfetch.fetch(
        url='<URL>',
        payload={user: <USER_KEY>, data: {infoa=123, infob=ABC, ...}},
        method=urlfetch.POST,
        headers=headers)

【问题讨论】:

    标签: python google-app-engine curl https


    【解决方案1】:

    所以: -u for user 进入授权标头。基本加密是base64。 -d 表示数据进入有效负载。不同的 -d 与一个简单的 & 符号连接。

       try:
            headers = {'Content-Type': 'application/x-www-form-urlencoded',
                       "Authorization": "Basic %s" % base64.b64encode(<some_private_user_authentication>)} # base64 or similar needs to be imported
            result = urlfetch.fetch(
                url='<URL of endpoint>',
                payload='infoa={}&infob={}&description=Test Transaction'.format(info_a,                                                                                         info_b),
                method=urlfetch.POST,
                headers=headers)
            print repr(result.content) # do things..
    
        except urlfetch.Error:
            logging.exception('Caught exception fetching url')
            print 'Caught exception fetching url' # do other things..
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-05-20
      • 2016-08-04
      • 2018-10-21
      • 2017-05-25
      • 2014-04-12
      • 2021-04-28
      • 2021-12-05
      相关资源
      最近更新 更多