python - convert requests.models.Response to Django HttpResponse -
in django project, need get/post data third-party url in view, , redirect web page provides. example, can
class testview(templateview): def get(self, request, *args, **kwargs): data = { 'order_id': 88888, 'subject': 'haha', 'rn_check': 'f', 'app_pay': 't', } url = 'http://some-third-party-api-url?order_id=88888&subject=haha&...' return httpresponseredirect(url)
however want use third-party api wrapped sdk ,
class testview(templateview): def get(self, request, *args, **kwargs): sucre.alipay_sdk.base import alipay sucre.alipay_sdk import alipay_config django.http import httpresponse alipay = alipay(alipay_config) data = { 'order_id': 88888, 'subject': 'haha', 'rn_check': 'f', 'app_pay': 't', } '''alipay api wrapped in sdk''' '''and return requests.models.response instance''' result = alipay.api('pay', data) return httpresponse(result)
and api code:
def api(self, service, data): ''' logics here ''' import requests response = requests.get(url, data=data) return response
but seems httpresponse(result) not correct way convert requests.models.response instance httpresponse... layout bad, , more encoding issues, etc...is there correct way convert requests response django httpresponse?
updates:
httpresponse(result) worked, css of page lost. might related using requests.
this should works:
from django.http import httpresponse import requests requests_response = requests.get('/some-url/') django_response = httpresponse( content=requests_response.content, status=requests_response.status_code, content_type=requests_response.headers['content-type'] ) return django_response
Comments
Post a Comment