dan StDi
dan StDi

Reputation: 21

Send POST HTTP request to google calendar/v3 API return parse error

I'm trying to send HTTP requests to the Google Calendar/v3 API using the REST client library components in DELPHI XE5 Update2(TRESTClient, TRESTRequest, TRESToAuth2Autenticator, TRESTResponse). After settings this components in Delphi IDE, GET requests is working well, returning desired JSON response.

But POST request (https://www.googleapis.com/calendar/v3/calendars/primary/events) with a parameter in TRESTRequest.params setting up as pkREQUESTBODY with option poDoNotEncode and a value of:

{
   "end":{
      "date":"2014-06-13"
   },
   "start":{
      "date":"2014-06-13"
   },
   "summary":"reTest"
}

returns a Google API error:

{
   "error":{
      "errors":[
         {
            "domain":"global",
            "reason":"parseError",
            "message":"This API does not support parsing form-encoded input."
         }
      ],
      "code":400,
      "message":"This API does not support parsing form-encoded input."
   }
}

This code and parameters is working well in DELPHI XE6 , not in XE5.

procedure TForm2.Button1Click(Sender: TObject);
var
  rBody: TStringStream;
begin
  rBody := TStringStream.Create('{"end": {"date": "2014-06-13"},"start": {"date": "2014-06-13"},"summary": "reTest"}');
  RESTRequest.AddBody(rBody, ctAPPLICATION_JSON);
  RESTRequest.Execute;
end;  

Is there someting I'm doing wrong , how can I do to get the request working ? Thanks for your help.

Upvotes: 2

Views: 4805

Answers (2)

Remy Lebeau
Remy Lebeau

Reputation: 598011

You are not setting up the RESTRequest correctly. Most likely, you are not setting the request body's ContentType to ctAPPLICATION_JSON. By default, a single-parameter RESTRequest will use ctAPPLICATION_X_WWW_FORM_URLENCODED unless you specify otherwise. This is stated in the documentation:

REST.Client.TRESTRequestParameter.ContentType:

When this parameter is left empty, the content type will be chosen basically depending on the number of existing parameters, that go into the requests body. A single-parameter request uses application/x-www-form-urlencoded, while a multiple-parameter request uses multipart/mixed instead.

That would explain why the server is complaining about the data being form-encoded. It is looking at the body's Content-Type header, sees an unsupported value, and reports an error back to you ignoring the actual body data you sent.

Upvotes: 3

mjn
mjn

Reputation: 36664

Based on the API description at

https://developers.google.com/google-apps/calendar/v3/reference/events/insert

it looks like the JSON part belongs in the POST request body.

The error message sounds like the JSON part is encoded as a HTML form field value (you can verify this with a HTTP proxy like Fiddler2).

Hint: If you have a working client based in a different language, you can use Fiddler2 to compare the working and the non-working requests to find out what is wrong.

Upvotes: 0

Related Questions