如何使用UrlFetchApp服务发送JSON有效负载? [英] How to send a JSON payload with UrlFetchApp service?

查看:109
本文介绍了如何使用UrlFetchApp服务发送JSON有效负载?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试过帐到期望使用Google Apps脚本获取JSON作为有效载荷的Web服务.我正在使用以下代码:

I'm trying to POST to a web service that is expecting to get JSON as payload using Google Apps Script. I'm using the following code:

var options =
{
  "method" : "post",
  "contentType" : "application/json",
  "headers" : {
    "Authorization" : "Basic <Base64 of user:password>"  
  },
  "payload" : { "endDate": "2012-06-03" }
};

var response = UrlFetchApp.fetch("http://www.example.com/service/expecting/json", options);

在服务器上,我收到以下错误消息:

On the server side I'm getting the following error:

WARN [facade.SettingsServlet] 04 Jun 2012 15:30:26 - Unable to parse request body: endDate=2012-06-03
net.liftweb.json.JsonParser$ParseException: unknown token e

我假设服务器期望得到

{ "endDate": "2012-06-03" }

代替

endDate=2012-06-03

但是我不知道如何使UrlFetchApp做到这一点.

but I don't know how to make the UrlFetchApp do it.

推荐答案

我不理解服务器端错误,但是'payload'参数必须是在此处指定的字符串:

I do not understand the server side error but the 'payload' parameter must be a string as specified here: https://developers.google.com/apps-script/class_urlfetchapp?hl=fr-FR#fetch.

尝试:

var options =
{
  "method" : "post",
  "contentType" : "application/json",
  "headers" : {
    "Authorization" : "Basic <Base64 of user:password>"  
  },
  "payload" : '{ "endDate": "2012-06-03" }'
};

这篇关于如何使用UrlFetchApp服务发送JSON有效负载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆