2017-04-25 3 views
1

플라스크에서 게시물 요청을 보내려고합니다.플라스크에서의 게시물 요청 전송

Content-Type: application/json으로 json 개체를 헤더로 보내고 싶습니다. 다음과 같이

나는 요청 모듈을이 일을 해요 :

json_fcm_data = {"data":[{'key':app.config['FCM_APP_TOKEN']}], "notification":[{'title':'Wyslalem cos z serwera', 'body':'Me'}], "to":User.query.filter_by(id=2).first().fcm_token} 
json_string = json.dumps(json_fcm_data) 
print json_string 
res = requests.post('https://fcm.googleapis.com/fcm/send', json=json_string) 

을하지만이 나 있습니다 :

TypeError: request() got an unexpected keyword argument 'json'

이 문제를 해결하는 방법에 대한 조언은?

+0

패스'데이터 = json_string' –

+0

그러나 "콘텐츠 형식 : 응용 프로그램/JSON"을 설정합니까 : 전체 솔루션이 될 것인가? – demoo

+1

'headers','param' 및'data'의 세 가지 속성이 있습니다. 'content-type'과 같은 헤더 변수를 설정하려면 header 속성에 추가해야합니다. –

답변

2

먼저 오류 수정이에

res = requests.post('https://fcm.googleapis.com/fcm/send', json=json_string) 

:

res = requests.post('https://fcm.googleapis.com/fcm/send', data=json_string) 

오류를 requests.post 받아 들일 수없는 당신이 상태를지고 당신은이를 변경해야

을 인수는 json이지만 키워드 arg를 허용합니다. 이름이 data이고 json 형식 일 수 있습니다.

그런 다음 헤더를 추가 :

을 당신이 requests 모듈과 사용자 정의 헤더를 보내려면 다음과 같이 당신이 그것을 할 수 있습니다

headers = {'your_header_title': 'your_header'} 
# In you case: headers = {'content-type': 'application/json'} 
r = requests.post("your_url", headers=headers, data=your_data) 

모든 것을 요약하면 최대 :

json 형식을 약간 수정해야합니다.

json_data = {"data":{ 
       'key':app.config['FCM_APP_TOKEN'] 
       }, 
      "notification":{ 
       'title':'Wyslalem cos z serwera', 
       'body':'Me' 
       }, 
      "to":User.query.filter_by(id=2).first().fcm_token 
      } 

headers = {'content-type': 'application/json'} 
r = requests.post('https://fcm.googleapis.com/fcm/send', 
        headers=headers, 
        data=json.dumps(json_data)) 
+0

그것은 솔기가 작동합니다 <3 Thank you! JSON 객체를 올바르게 수행하고 있는지 확인할 수 있습니까? { "제목": { "데이터": { "키": "값" } "통지" "안녕", "몸"이 같은 일을 좀하고 싶습니다 " 확인 " }, "to ":"token " } – demoo

+1

내 대답에 대한 전체 솔루션을 추가했습니다. –

+0

"JSON_PARSING_ERROR : 위치 0에 예상치 못한 문자 (d)가 있습니다. \ n" – demoo