python
  • google-app-engine
  • curl
  • https
  • 2017-11-24 4 views 0 likes 
    0

    私のgae-serverからapiにhttpsリクエストを発行したいとします。 urlfetchを使用します。 callの例はcurlコマンドです。curlコマンドをurlfetchまたは一般的にリクエストするにはどうすればいいですか?

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

    私が知りたいすべてがHTTPS要求がどのようになるか、であるので、私はthisドキュメントを使用して、それを複製することができます。おそらくそれについて間違っていますが、私は--trace-ascii -をカールで使用しましたが、ouptutからはまだ100%は私が発行しているような要求を言うことはできません。
    http-requestのどの部分を翻訳しますか?この仕事のようなものでしょうか:

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

    答えて

    0

    So:ユーザ用の-uは認証ヘッダに入ります。基本的な暗号化は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.. 
    
    関連する問題