简体   繁体   English

用于创建问卷调查的JSON字符串有效时,JSON字符串不正确时,SurveyMonkey API Error400

[英]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.

相关问题 JSON错误字符串错误 - JSON Bad String Error SurveyMonkey API是否有json模式? - Is there a json schema for the SurveyMonkey API? 当json请求我收到400 Bad Request错误时,Spring3不起作用@valid - Spring3 doesn't work @valid when json request I got 400 Bad Request error JSON 数组中的长字符串导致 .NET 核心中的 400 错误请求 - Long string in JSON array causes 400 Bad Request in .NET Core Web API 从客户端向服务器发送JSON字符串时出现400错误的请求异常 - 400 Bad request exception when send JSON string to server from client 在查询标签Retrofit 2 Android中使用Json字符串参数时,响应代码400错误请求 - Response Code 400 Bad Request when use Json String param in Query tag Retrofit 2 Android 在C#中将JSON字符串发布到RESTful WCF服务时,返回400(错误请求)或空数据 - 400 (Bad Request) or empty data when POSTing JSON string to RESTful WCF service in C# 使用Python将JSON对象发送到api时出现错误“ 400错误请求” - Error “400 Bad request” when sending JSON object to an api using Python 在JavaScript中发布到Restful API时,JSON中出现Bad Request Error(400) - Bad Request Error (400) in json while posting to restful api in javascript Steam API JSON 仅在空时返回错误请求 400 - Steam API JSON returning bad request 400 only when empty
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM