[英]SurveyMonkey API Error400 on bad JSON string when JSON string for creating survey poll is valid
I was looking into ways to create surveys programmatically and came across SurveyMonkey APIs. 我正在研究以编程方式创建调查的方法,并遇到了SurveyMonkey API。 I manually created an empty survey on my account and created a draft app and got an access token from there.
我在我的帐户上手动创建了一个空调查,并创建了一个草稿应用,并从那里获得了访问令牌。 And then I copied & pasted the example code from the API doc on the section of creating a survey question to try it out.
然后,我在创建调查问题的部分中复制并粘贴了API文档中的示例代码以进行尝试。
You can also see the code here where the survey ID and the page ID were obtained by using the /surveys
and /surveys/{id}/pages
end points. 您还可以在此处查看通过使用
/surveys
和/surveys/{id}/pages
端点获取调查ID和页面ID的代码。
However, I get this following error. 但是,出现以下错误。
{u'error': {u'docs': u'https://developer.surveymonkey.com/api/v3/#error-codes', u'message': u'The body provided was not a proper JSON string.', u'http_status_code': 400, u'id': u'1001', u'name': u'Bad Request'}}
I checked that the JSON object is valid and contains all the required parameters as specified in the doc. 我检查了JSON对象是否有效,并包含了文档中指定的所有必需参数。 I also tried some smaller, simpler questions and still no success.
我还尝试了一些更小,更简单的问题,但仍然没有成功。 If I passed in an empty JSON object, it does complain about the missing required parameters.
如果我传入一个空的JSON对象,它会抱怨缺少必需的参数。 I was wondering if someone could point out what I'm doing wrong.
我想知道是否有人可以指出我做错了什么。 Thank you!
谢谢!
The issue is you aren't actually sending the body as json properly. 问题是您实际上没有正确地将正文作为json发送。
url = "https://api.surveymonkey.net/v3/surveys/%s/pages/%s/questions" % (survey_id, page_id)
r = s.post(url, data=payload)
print r.json()
You need to json.dumps(payload)
您需要
json.dumps(payload)
import json
url = "https://api.surveymonkey.net/v3/surveys/%s/pages/%s/questions" % (survey_id, page_id)
r = s.post(url, data=json.dumps(payload))
print r.json()
In newer version of the requests
library you can use the json
kwarg for that. 在较新版本的
requests
库中,您可以使用json
kwarg。
url = "https://api.surveymonkey.net/v3/surveys/%s/pages/%s/questions" % (survey_id, page_id)
r = s.post(url, json=payload)
print r.json()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.